我的项目有两个文件夹:folder1
和folder2
。
在folder1
中,我尝试使用其引用路径加载folder2中存在的文件,但它给出了以下异常:java.io.FileNotFoundException: /folder2/blah.txt (No such file or directory)
。它在我使用绝对路径时有效。
file = new FileInputStream("/folder2/blah.txt")
答案 0 :(得分:1)
您应该始终通过调用应用程序的根路径来调用路径。我假设folder1和folder2不相关,这意味着,folder2不在folder1中,这就是它发生的原因。
请检查How to get the real path of Java application at runtime?以获取根路径,然后您可以根据需要访问目录
在其他方面,您可以尝试:
String path = new File(".").getCanonicalPath();
file = new FileInputStream( path + "/folder2/blah.txt"); // I have not tested here. If it returns an error try removing the / from the beginning of the folder2
答案 1 :(得分:-1)
这是一个非常简单的修复。您正尝试在/folder2/blah.properties中加载系统中的文件。但是,如果它只是在Java项目的类路径中,则会省略初始文件路径分隔符。
尝试这样的事情:
//Initial seperator is oitted.
file = new FileInputStream("folder2/blah.txt")