很多人遇到“说文件不存在,但它确实存在”的问题,但我的问题恰恰相反,文件不存在,但它说它存在。 不确定如何解决这个问题和其他主题只是提出“文件不存在但它存在”等。
这是我的代码:
package New;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class FileEditor {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
System.out.println("Where is the file stored ex: C:/Users/Name/Place/filename.txt");
String a = scan.nextLine();
File file = new File(a);
FileWriter writer = new FileWriter(file);
BufferedWriter bwriter = new BufferedWriter(writer);
if(!file.exists()){
System.out.println("File does not exist.");
}
else{
System.out.println("Start editing? y/n");
String b = scan.nextLine();
最佳答案
在验证文件存在之前不要创建 FileWriter/BufferedWriter。
关于java - 文件不存在但说它存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35374685/