PDF to Image is reported java.io.FileNotFoundException: F: \ WangJun (Access is denied.)

Copyright: personal essays, problems at work, only to save the document, hoping to help you, please correct me if wrong with progress! Thank you! https://blog.csdn.net/w893932747/article/details/89399152

Error:

One of the problems
when using FileInputStream or FileOutputStream encounter the following questions 1 and 2.
Question 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)

Question 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)

Second, the analysis
during analysis, I have to say clearly when throwing denied access when thrown find the path specified. The reason is that, when constructing a File object specifying the file path is anything can be, if not there is also possible to configure the File object, but now you are going to file input and output operations, that is, when the InputStream and OutputStream operation, If you fill in the path does not exist, it will report system can not find the path specified, if the specified directory is, will be reported to refuse access exception. After reading this premise, the read on.

When faced with a problem, indeed the currently specified file or directory does not exist does not exist.
When faced with the second question, because you are accessing a file directory, if this directory does not have access or the directory does not exist, an exception will be thrown issue 2.

Third, the solution to
the first solution is to first determine what the current file exists, if there is skipped, if not, in the creation, specific practices are as follows:

// When completing the file path, must write on a specific file name (xx.txt), otherwise there will be denied access.

File file = new File("./mywork/work.txt");
if(!file.exists()){
    //先得到文件的上级目录,并创建上级目录,在创建文件
    file.getParentFile().mkdir();
    try {
        //创建文件
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The second solution is, in completing the path to the file must be specific to the file, as follows:

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

And not be written as:

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

Because you are accessing a directory, so access is denied.

Fourth, the source (my demo)

1, file output stream

/**
 * 文件输出流方法
 */
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();
    }

}

2, file input stream Method

/**
 * 文件输入流
 */
public void fileInputStream() {
    File file = new File("./mywork/work.txt");
    FileInputStream in = null;
    //如果文件不存在,我们就抛出异常或者不在继续执行
    //在实际应用中,尽量少用异常,会增加系统的负担
    if (!file.exists()){
        throw new FileNotFoundException();
    }
    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();
    }
}

This man was very good summary, moved directly to his notes, but also solve my problem! https://blog.csdn.net/YQS_Love/article/details/51959776

Guess you like

Origin blog.csdn.net/w893932747/article/details/89399152