我有一个赋值,我们有几个类给出,其中一个是文件读取器类,它有一个读取文件的方法,并使用包含文件路径的参数(String)调用,现在我有几个. txt文件,它们与.java文件在同一个文件夹中,所以我想我可以将file.txt作为文件路径传递(比如在php中,相对而言),但总是返回一个未找到的文件异常!
看到给定的类应该正常工作的事实,并且我确认这些类实际上与.java文件位于相同的文件夹workspace/src中我必须对文件路径字符串做错了什么,但是什么?
这是我的代码:
private static final String fileF = "File.txt";
private static final ArrayList<String[]> instructionsF =
CreatureReader.readInstructions(fileF);
Run Code Online (Sandbox Code Playgroud)
Gor*_*vic 10
把这个:
File here = new File(".");
System.out.println(here.getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)
你代码中的某个地方.它将打印出您程序的当前目录.
然后,只需将文件放在那里,或更改文件路径.