读取使用java在运行时创建的文件
我用一个java方法创建并编写一个文件,然后我想在运行时用另一个java方法读取这个文件。但它抛出了java。木卫一。FileNotFoundException错误
如何修复此错误
Writer output=null;
File file = new File("train.txt");
output = new BufferedWriter(new FileWriter(file));
output.write(trainVal[0] + "\n");
-------------------
and read code
FileInputStream fstreamItem = new FileInputStream("train.tx");
DataInputStream inItem = new DataInputStream(fstreamItem);
BufferedReader brItem = new BufferedReader(new InputStreamReader(inItem));
String phraseItem;
ArrayList<Double> qiF = new ArrayList<Double>();
while ((phrase = br.readLine()) != null) {
//doing somethinh here
}
# 1 楼答案
使用正确的文件名。这包括文件的路径。还要确保没有人删除这两个函数之间的文件或重命名它
# 2 楼答案
以下是读取文件的最佳且方便的方法之一。通过它而不是使用传统的方法
您在代码中提供了正确的文件扩展名,这是一个错误
应该是