在另一个目录中为一个bufferedReader选择一个文件 [英] Selecting a file in another directory for a bufferedReader
问题描述
Project / library / initialize.java
项目/资源/文本/ fileInput.txt
我在initialize.java中的代码是:
FileInputStream fstream = new FileInputStream(/ resources / text / fileInput.txt);
但是无法读取该文件。我也试过
FileInputStream fstream = new FileInputStream(./ resources / text / fileInput.txt);
但是没有这样做。
如何访问txt文件,使用./resources和/ resources有什么区别?
感谢您阅读本文。
差异很大。在 linux / unix / macos 以/从根目录开始的路径。以./开头的路径,或从当前应用程序目录开始。调用
System.out.println(new File(。)。getAbsolutePath())
你是吗?
I have an initialization file (initialize.java) that pulls in data from fileInput.txt using a fileInputStream, but both of them are in different directories.
Project/library/initialize.java Project/resources/text/fileInput.txt
my code in initialize.java is:
FileInputStream fstream = new FileInputStream("/resources/text/fileInput.txt");
But the file cannot be read. I've also tried
FileInputStream fstream = new FileInputStream("./resources/text/fileInput.txt");
But that didn't work too.
How can I access the txt file and what's the difference between using "./resources" and "/resources"?
Thanks for reading this.
The difference is huge. On linux/unix/macos the path starting with / starts from root directory. The path starting with ./ or without starting from current application directory. Call
System.out.println(new File("."). getAbsolutePath())
to check where you are?
这篇关于在另一个目录中为一个bufferedReader选择一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!