这可能是一个简单的错误,但我被困住了。我将一个 .dat 文件与我想要在程序中读取的文本保留在与主类和基类相同的包中,并且我得到了 FileNotFoundException。我尝试更改 .txt 上的文件,获取 URI 语法,但这没有帮助。重点在哪里?
主类:
package syseks;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
public class main {
public static syseks.base ba;
public main(syseks.base ba){
ba = this.ba;
}
public static void main(String[] args) throws FileNotFoundException {
// TODO code application logic here
ba = new base();
String[] py = ba.loadData('p');
System.out.print(py);
}
}
以及基类的 loadData 方法:
public String[] loadData(char param) throws FileNotFoundException{
List<String> strlist = new ArrayList<String>();
if(param == 'p'){
Scanner sc = new Scanner(new File("py.dat")); //HERE'S THE CRASH
while(sc.hasNextLine()){
strlist.add(sc.nextLine());
}
}
else{
Scanner sc = new Scanner(new File("wy.dat"));
while(sc.hasNextLine()){
strlist.add(sc.nextLine());
}
}
String[] da = strlist.toArray(new String[0]);
return da;
}
最佳答案
向 File
构造函数参数提供绝对路径。像这样的东西:
Scanner sc = new Scanner(new File("/home/foo/proj/myapp/src/syseks/py.dat"));
关于java - .dat 文件上出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28015357/