【发布时间】:2015-12-08 10:52:41
【问题描述】:
public static void update(String fileName, String idType, String id, String updatedData[] ) throws Exception{
char fileOutput[];
String wholeData;
String tkn, idtp, idf;
File myFileU = null;
File tempFile = null;
FileReader finU = null;
FileWriter fwU = null;
Scanner frU = null;
try{
finU = new FileReader(myFileU = new File("\\" +fileName + ".txt"));
fileOutput = new char[(int) myFileU.length()];
finU.read(fileOutput);
finU.close();
//System.out.println(myFileU.getCanonicalPath());
tempFile = new File("temp.txt");
tempFile.createNewFile();
fwU = new FileWriter(myFileU, false);
wholeData = new String(fileOutput);
frU = new Scanner(wholeData);
frU.useDelimiter(";");
while(frU.hasNext()){
idtp = frU.next();
idf = frU.next();
if(idtp.equals(idType) && idf.equals(id)){
fwU.write( ";" + idType + ";" + id);
for(int i=0; i< updatedData.length; i++){
fwU.write(";" + updatedData[i]);
}
fwU.write(";" + System.lineSeparator());
frU.nextLine();
}
if(!idf.equals(id))
fwU.write(";" + idtp + ";" + idf);
tkn = frU.nextLine();
fwU.write(tkn);
fwU.write(System.lineSeparator());
if(idf.equals(autoSerial(fileName, idType)))
break;
}
fwU.flush();
fwU.close();
}
catch(IOException e){
System.out.println("error in opening the file U " + e);
}
finally{
}
}
上述方法旨在覆盖它正在读取的文件。它应该做的是从文件中读取,用更新的数据替换用户指定的记录并用更新的数据覆盖文件,但它不会覆盖文件而是将更新的记录附加到文件的末尾和给出(尽管如果我将数据保存到单独的文件中,它会将更新后的数据正确保存到其中):
java.io.FileNotFoundException: \Schedules.txt (The system cannot find the file specified)
当文件在那里并且它也从中读取数据时?有什么线索吗?我是 Java 新手!
【问题讨论】:
-
您的文件在哪里?您的文件的完整位置?它给出了异常,因为它找不到它。如果文件位于同一目录中,则删除 \\ 并重试 File(fileName + ".txt")
-
有一个使用反斜杠的操作系统不是Windows?有趣。
标签: java file-io filenotfoundexception