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

来源:互联网 发布:卡通农场数据丢失 编辑:程序博客网 时间:2024/06/08 20:42

转自 : 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

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

阅读全文
0 0