Java,从当前目录读取文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1480398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java, reading a file from current directory?
提问by Saobi
I want a java program that reads a user specified filename from the current directory (the same directory where the .class file is run).
我想要一个 java 程序,它从当前目录(运行 .class 文件的同一目录)读取用户指定的文件名。
In other words, if the user specifies the file name to be "myFile.txt", and that file is already in the current directory:
换句话说,如果用户将文件名指定为“myFile.txt”,并且该文件已经在当前目录中:
reader = new BufferedReader(new FileReader("myFile.txt"));
does not work. Why?
不起作用。为什么?
I'm running it in windows.
我在 Windows 中运行它。
采纳答案by Laurence Gonsalves
The current directory is not (necessarily) the directory the .class file is in. It's working directory of the process. (ie: the directory you were in when you started the JVM)
当前目录不是(必然).class 文件所在的目录。它是进程的工作目录。(即:启动 JVM 时所在的目录)
You can load files from the same directory*as the .class file with getResourceAsStream(). That'll give you an InputStream which you can convert to a Reader with InputStreamReader.
您可以使用getResourceAsStream()从与.class 文件相同的目录*加载文件。这将为您提供一个 InputStream ,您可以使用InputStreamReader将其转换为 Reader 。
*Note that this "directory" may actually be a jar file, depending on where the class was loaded from.
*请注意,这个“目录”实际上可能是一个 jar 文件,具体取决于类的加载位置。
回答by Joonas Pulakka
Try
尝试
System.getProperty("user.dir")
It returns the current working directory.
它返回当前工作目录。
回答by Joel Westberg
If you know your file will live where your classes are, that directory will be on your classpath. In that case, you can be sure that this solution will solve your problem:
如果您知道您的文件将位于您的类所在的位置,则该目录将位于您的类路径中。在这种情况下,您可以确定此解决方案将解决您的问题:
URL path = ClassLoader.getSystemResource("myFile.txt");
if(path==null) {
//The file was not found, insert error handling here
}
File f = new File(path.toURI());
reader = new BufferedReader(new FileReader(f));
回答by Tony Vu
None of the above answer works for me. Here is what works for me.
以上答案都不适合我。这对我有用。
Let's say your class name is Foo.java, to access to the myFile.txt in the same folder as Foo.java, use this code:
假设您的类名是 Foo.java,要访问与 Foo.java 相同文件夹中的 myFile.txt,请使用以下代码:
URL path = Foo.class.getResource("myFile.txt");
File f = new File(path.getFile());
reader = new BufferedReader(new FileReader(f));
回答by katwekibs
Files in your project are available to you relative to your src folder. if you know which package or folder myfile.txt will be in, say it is in
相对于 src 文件夹,您可以使用项目中的文件。如果您知道 myfile.txt 将在哪个包或文件夹中,请说它在
----src
--------package1
------------myfile.txt
------------Prog.java
you can specify its path as "src/package1/myfile.txt" from Prog.java
您可以从 Prog.java 指定其路径为“src/package1/myfile.txt”
回答by Ashwin
Try this:
尝试这个:
BufferedReader br = new BufferedReader(new FileReader("java_module_name/src/file_name.txt"));
回答by Charlie
try using "." E.g.
尝试使用“。” 例如
File currentDirectory = new File(".");
This worked for me
这对我有用