简体   繁体   English

使用FileReader时如何写文件路径?

[英]How to write a file path when using FileReader?

I am having a hard time understanding how reading file works in java.我很难理解在 Java 中读取文件是如何工作的。 Now, I know how to write a code that reads a text file.现在,我知道如何编写读取文本文件的代码。 Here is what I have.这是我所拥有的。

BufferedReader file = new BufferedReader(new FileReader("/PATH_TO_THE_FILE"));

And this works very well.这非常有效。

Let's say "/PATH_TO_THE_FILE" is very specific to my computer.假设"/PATH_TO_THE_FILE"非常特定于我的计算机。 maybe it uses folder names that are different from other people, which also means this path is only valid on my computer, right?也许它使用的文件夹名称与其他人不同,这也意味着此路径仅在我的计算机上有效,对吗?

In this case, what would happen if I (sender) zip this java and the text file together and send it to another person?在这种情况下,如果我(发件人)将此 java 和文本文件压缩在一起并将其发送给另一个人会发生什么? My guess is that it won't work unless the receiver changes "/PATH_TO_THE_FILE" from the java file since it is written to work on the sender's computer.我的猜测是,除非接收者从 java 文件中更改"/PATH_TO_THE_FILE" ,否则它不会工作,因为它是为在发送者的计算机上编写的。

I am not sure if my explanation is clear.我不确定我的解释是否清楚。 Please let me know so I can make better edits!请让我知道,以便我进行更好的编辑!

Thanks谢谢

Yes, that's what would happen.是的,这就是会发生的事情。

You could pass the path to the file as an argument to the Java process.您可以将文件路径作为参数传递给 Java 进程。 Then it is part of the arguments passed to然后它是传递给的参数的一部分

public static void main(String[] args)

This would make your code more portable.这将使您的代码更具可移植性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM