[Solved] JAVA Beginners’ Error: Exception in thread “main“ java.io.FileNotFoundException

0x00 overview

During the practice in the Java IO flow chapter, an error was found when running the code, saying that the file path is incorrect.

0x01 solution

Error code

package FileDemo2;

import java.io.File;
import java.io.IOException;

public class FileDemo2 {
    public static void main(String[] args) throws IOException {
        // Requirement 1:Create a file java.txt in the C:\Users\119k\IdeaProjects\d17\FileDemo2\ directory
        File f1 = new File("C:\\Users\\119k\\IdeaProjects\\d17\\FileDemo2\\java.txt");
        System.out.println(f1.createNewFile());
        System.out.println("--------------");


        // Requirement 2:Create a directory in the C:\Users\119k\IdeaProjects\d17\FileDemo2\ directory
        File f2 = new File("C:\\Users\\119k\\IdeaProjects\\d17\\FileDemo2\\JavaSE\\");
        System.out.println(f2.mkdir());
        System.out.println("--------------");

        // Requirement 3:Create a multi-level directory in the C:\Users\119k\IdeaProjects\d17\FileDemo2\ directory \JavaSE\JavaEE\
        File f3 = new File("C:\\Users\\119k\\IdeaProjects\\d17\\FileDemo2\\JavaSE\\JavaEE\\");
        System.out.println(f3.mkdirs());
        System.out.println("--------------");

        // Requirement 4:Create a file javaSE.txt in the C:\Users\119k\IdeaProjects\d17\FileDemo2\ directory
        File f4 = new File("C:\\Users\\119k\\IdeaProjects\\d17\\FileDemo2\\javaSE.txt");
        System.out.println(f4.createNewFile());
        System.out.println("--------------");
    }
}

For the revised code, add \src

package FileDemo2;

import java.io.File;
import java.io.IOException;

public class FileDemo2 {
    public static void main(String[] args) throws IOException {
        // Requirement 1:Create a file java.txt in the C:\Users\119k\IdeaProjects\d17\FileDemo2\ directory
        File f1 = new File("C:\\Users\\119k\\IdeaProjects\\d17\\src\\FileDemo2\\java.txt");
        System.out.println(f1.createNewFile());
        System.out.println("--------------");


        // Requirement 2:Create a directory in the C:\Users\119k\IdeaProjects\d17\FileDemo2\ directory
        File f2 = new File("C:\\Users\\119k\\IdeaProjects\\d17\\src\\FileDemo2\\JavaSE\\");
        System.out.println(f2.mkdir());
        System.out.println("--------------");

        // Requirement 3:Create a multi-level directory in the C:\Users\119k\IdeaProjects\d17\FileDemo2\ directory \JavaSE\JavaEE\
        File f3 = new File("C:\\Users\\119k\\IdeaProjects\\d17\\src\\FileDemo2\\JavaSE\\JavaEE\\");
        System.out.println(f3.mkdirs());
        System.out.println("--------------");

        // Requirement 4:Create a file javaSE.txt in the C:\Users\119k\IdeaProjects\d17\FileDemo2\ directory
        File f4 = new File("C:\\Users\\119k\\IdeaProjects\\d17\\src\\FileDemo2\\javaSE.txt");
        System.out.println(f4.createNewFile());
        System.out.println("--------------");
    }
}

The absolute path of the file needs to be obtained in the IDE. Here, copy-absoulte path is used to find that there is more \\src in the file path

Similar Posts: