Java解决java.io.FileNotFoundException: E:\work\work (拒绝访问。)

5 篇文章 0 订阅

转自 : http://blog.csdn.net/yqs_love/article/details/51959776


一、问题 
在使用FileInputStream时会遇到如下问题1和问题2。 
问题1:

java.io.FileNotFoundException: .\xxx\xxx.txt (系统找不到指定的路径。)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at com.yaohong.test.InputStreamTest.fileInputStream(InputStreamTest.java:13)
    at com.yaohong.test.InputStreamTest.main(InputStreamTest.java:27)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

问题2:

java.io.FileNotFoundException: .\xx\xx (拒绝访问。)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at com.yaohong.test.InputStreamTest.fileInputStream(InputStreamTest.java:13)
    at com.yaohong.test.InputStreamTest.main(InputStreamTest.java:27)
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

二、分析 
当遇到问题1时,的确是当前所指定的文件不存在或者目录不存在。 
当遇到第二个问题时,是因为你访问了一个文件目录,而不是文件,因此会抛出问题2的异常。

三、解决办法 
第一个的解决办法是,先判断一下当前文件是否存在,如果存在则略过,如果不存在,在创建,具体做法如下:

//在填写文件路径时,一定要写上具体的文件名称(xx.txt),否则会出现拒绝访问。
File file = new File("./mywork/work.txt");
if(!file.exists()){
    //先得到文件的上级目录,并创建上级目录,在创建文件
    file.getParentFile().mkdir();
    try {
        //创建文件
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

第二个的解决办法是,在填写文件的路径时一定要具体到文件,如下:

File file = new File("./mywork/work.txt");
 
 
  • 1

而不能写成:

    File file = new File("./mywork/");
 
 
  • 1

因为这样你访问的是一个目录,因此就拒绝访问。

四、源码(我的demo)

1、文件输出流

/**
 * 文件输出流方法
 */
public void fileOutputStream() {
    File file = new File("./mywork/work.txt");
    FileOutputStream out = null;
    try {
        if (!file.exists()) {
            // 先得到文件的上级目录,并创建上级目录,在创建文件
            file.getParentFile().mkdir();
            file.createNewFile();
        }

        //创建文件输出流
        out = new FileOutputStream(file);
        //将字符串转化为字节
        byte[] byteArr = "FileInputStream Test".getBytes();
        out.write(byteArr);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

2、文件输入流方法

/**
 * 文件输入流
 */
public void fileInputStream() {
    File file = new File("./mywork/work.txt");
    FileInputStream in = null;
    //如果文件不存在,我们就抛出异常或者不在继续执行
    //在实际应用中,尽量少用异常,会增加系统的负担
    if (!file.exists())
        new FileNotFoundException();
        //return;
    try {
        in = new FileInputStream(file);
        byte bytArr[] = new byte[1024];
        int len = in.read(bytArr);
        System.out.println("Message: " + new String(bytArr, 0, len));
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

如有错误,还望指正,谢谢合作。

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值