1

Edit: If you have landed on this question, chances are you are unfamiliar with the concept - working directory.

I don't know if this is specific to IntelliJ IDEA's current working directory or I do not understand the concept of relative files well enough. I did come to a conclusion that solves my problem, but it leaves a lot of things unanswered for me, I don't like to just memorize stuff, I want to understand it. That is why I am asking this question here, thank you in advance.

Let's say you have

  1. a Class called Main
  2. a text file called text1.txt

and they are both located in the folder src

In the Main class you have written the following code

public class Main {

    public static void main(String[] args) {        
        // Scanner
        Scanner scanner = new Scanner(System.in);       
        // File object
        File myFile = new File("text1.txt");
        // Prints a String, that tells you if the file exists
        System.out.println("File exists = " + myFile.exists());
    }

}

Result: File exists = false

Why does this happen?

The question has received a good answer already, however, if you find this is not enough for you, then read the article below, it goes more in-depth.

https://www.earthdatascience.org/courses/intro-to-earth-data-science/python-code-fundamentals/work-with-files-directories-paths-in-python/

  • 1
    *"and they are both located in the folder "src""* - Well, that's your problem. The `text1.txt` file needs to reside within the working directory. Keep in mind, when the program is exported, the contents of `src` won't be available to your program, instead, in that context (and depending on the build system and IDE) it will become and embedded resource – MadProgrammer Jan 27 '22 at 22:22
  • To add to your question - from your code this issue is resolved if you write to the file before you check if it exists. – Alias Cartellano Jan 27 '22 at 22:31
  • 2
    Does this answer your question? [Java Cannot find file?](https://stackoverflow.com/questions/26056171/java-cannot-find-file) – Alexander Ivanchenko Jan 30 '22 at 23:22

1 Answers1

2

An absolute path starts from the filesystem root. Compare it to an address on a letter. The postman knows where to deliver that letter.

A relative path is not a target address. It is more like - when you see the gas station, turn left. Depending from which direction you come, you end up in various other locations.

Back to computers: relative paths are calculated based on a current working directory. You can print that from your java program by checking

How to get the current working directory in Java?

I usually write my code to be a bit clearer about the events. The following code will not only tell whether it found the file but also let you know where exactly it was searching for it.

        // File object
        File myFile = new File("text1.txt");

        // Prints a String, that tells you if the file exists
        System.out.println("File "+myFile.getAbsolutePath()+" exists = " + myFile.exists());
Queeg
  • 7,748
  • 1
  • 16
  • 42
  • So, I should always check the working directory before writing relative paths using only file names, because depending on the IDE and other things, it could be different in each scenario. Also I should not associate a working directory with a parent directory. Another, quicker solution would be to always write at least the parent folder of the file, for example "src/text1.txt", correct me if I am wrong. –  Jan 27 '22 at 22:41
  • 1
    I think you understood. Everything except the last part about the parent folder is right. Even if you use src/text1.txt it is a relative path and the current working directory will be used. Just to be clear: relative paths are not bad. They have their value. But you need to know what they are relative to. – Queeg Jan 27 '22 at 22:49