java - 文件不存在但说它存在?

标签 java

很多人遇到“说文件不存在,但它确实存在”的问题,但我的问题恰恰相反,文件不存在,但它说它存在。 不确定如何解决这个问题和其他主题只是提出“文件不存在但它存在”等。

这是我的代码:

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/

相关文章:

java - 执行满足 2 个条件之一的 while 循环

java - HttpURLConnection URI 不能为空

java - Android 应用程序崩溃后重新启动

java - MVC模式;我的模型类需要用户输入,但我知道它不应该询问用户或 Controller

java - 管理多个 JVM

java - Quartz 状态作业报告

java - NNTP 客户端 - Java - 下载文章随机下载 3 个额外的垃圾字节

java - 给定表达式集的正则表达式匹配(提取最后一列元素)

java - 如何避免编写类似 "XXX!=null"或 "XXX!=null || XXX.isEmpty"的代码

java - CloseableHttpAsyncClient 的超时异常