0

I'm not entirely sure if this is a Java issue or an IntelliJ issue, but I had a quick question about my File not being found via a path. My Main class is in the same directory as my input.txt file.

I though I should be able to do File file = new File("./input.txt"), except I get FileNotFoundException. When I do something like File file = new File("src/input.txt"), it does work.

I get that this may be a solution, but if I try to run this code outside of IntelliJ without a src directory, this will lead to an error where the File can't be found.

Is there any reason why I can't just do ./input.txt to specify that the File is in the same directory as the Main file?

nalabof679
  • 59
  • 1
  • 6
  • 1
    https://stackoverflow.com/questions/1480398/java-reading-a-file-from-current-directory – Dan Rayson Feb 10 '23 at 00:19
  • 2
    Paths are relative to the current working directory, not the source file that happen to be processed in. – Brian61354270 Feb 10 '23 at 00:37
  • Write a simple Code: ` File f1=new File("."); System.out.println(f1.getAbsolutePath());` , then goto Your Project Directory **out/production/YOUR_PROJECT** , Run `java Main` (if your class is Main.class), you can test it yourself. And next, your can package your class to a executeable jar, test again. – life888888 Feb 10 '23 at 02:07

2 Answers2

0

That's because in IDEA, the working directory of your program is the root directory of your project, not src by default. Therefore, you can access the file by ./src/input.txt or src/input.txt, not input.txt. By the way, it's bad practice to put your resources in src, because it gets mixed with your source code, and it won't be included in the JAR file, which means you cannot access it when your project is packaged as a JAR file. If you are using maven, you can put input.txt inside the resources folder(src/main/resources), and access it by Main.class.getResource("/input.txt")(gives you a URL) or Main.class.getResourceAsStream("/input.txt")(gives you a InputStream) depending on your needs. So that the resources will always be available, no matter you are running the program in IDEA or from a JAR.

Frank Yang
  • 373
  • 3
  • 8
0

FileNotFoundException occurs when the file doesn't exist at the specified location.

File file = new File("./input.txt"), the current working directory (.) is being used as the file location. If you are running the code from an IDE, the current working directory is usually the project root directory, not the directory where the code file is located. So, if the input.txt file is located in the same directory as your code file, then you should use the relative path to it.

File file = new File("src/input.txt"), you are specifying the absolute path to the input.txt file, which is inside the src directory. This is why it works, as the file is guaranteed to exist at that location.

If the file is located in the same directory as your code file, you can use a relative path, and if it's in a different directory, you should use an absolute path to it.