gpt4 book ai didi

java - 尽管 canRead() 和 contains() 返回 true,但 FileNotFoundException

转载 作者:行者123 更新时间:2023-12-01 07:33:28 28 4
gpt4 key购买 nike

我这样做并向该文件写入一些内容。

fileSymbol = new File("D:\\TempFiles\\SymbolFile.xml");
fileSymbol.createNewFile();
prSymbol = new PrintWriter(fileSymbol);
while(//condition goes here){
prSymbol.write(text);
}

然后我尝试从此文件中读取内容,

FileReader fr = new FileReader(fileSymbol);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();

当我尝试运行时,出现以下异常

java.io.FileNotFoundException: fileSymbol (The system cannot find the file specified)

当我尝试

fileSymbol.canRead();
fileSymbol.exists();

两者都返回 true。

尽管我已经创建了该文件,但我不明白为什么会收到此错误。当我手动检查位置时,我可以看到我创建的文件。

PS:我刚刚提供了我的代码的代码片段。

最佳答案

在阅读文件之前添加以下内容

  prSymbol.close();

close前不需要调用flush()。引用JavaDoc - 关闭()

关于java - 尽管 canRead() 和 contains() 返回 true,但 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15264239/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com