【问题标题】:FileNotFound exception error even thought the file exists in eclipseFileNotFoundexception 错误即使文件存在于 eclipse
【发布时间】:2019-05-31 05:34:59
【问题描述】:

在运行我的 java 文件 io 程序时,我得到 FileNotFoundException

我尝试更改文件的目录以及 SO 中提到的大多数其他解决方案,但没有任何效果。

我的代码:

package com.HelloWorld;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test {
public static void main(String[] args) {
    FileWriter w=null;
    BufferedWriter bw=null;
    try {
        String s="welcome";
        String b="‪‪D:\\test.txt";
        w=new FileWriter(b);
        bw=new BufferedWriter(w);
        bw.write(s);
        bw.flush();
    }
    catch(IOException e)
    {
        System.out.println("exception caught"+e);
    }
    finally{
        try {
            if(bw!=null)
                bw.close();}
            catch(Exception e) {
                System.out.println("exception caught"+e);
            }
            try {
                if(w!=null)
                {
                    w.close();
                    System.out.println("success");
                }}
                catch(Exception e) {
                    System.out.println("exception caught"+e);
                } 
            }



}
}

【问题讨论】:

  • 您实际上有一个驱动器 D:,您有权写入其根目录吗?您是否尝试过改用"D:/test.txt"
  • 根据文件的权限属性,操作系统可能会阻止从文件读取和写入文件。
  • 是的,我确实有一个驱动器 D:但是当我尝试“D:/test.txt”时,程序运行但它没有写入文件
  • 尝试将 b 包装到 File 对象中 - new File(b) - 并查看 api 提供的各种方法。从那里您应该能够确定文件是否存在并且可以写入。

标签: java eclipse file-handling java-io


【解决方案1】:

我已经在 D 盘中创建了文件,所以 FileWriter 覆盖了已经创建的文件名,因此它没有写入文件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-12
    相关资源
    最近更新 更多