I am trying to fetch a txt file from a specific folder not the same as that of the class in which I am trying to fetch using this simple code:
import java.io.File;
public class MyClass{
private static File file = new File("/folder1/folder2/myFile.txt");
public static void main(String[ ] args) {
//And trying to check if it exists
if(file.exists()) {
System.out.println(file.getName() + "exists!");
}
else {
System.out.println("The file does not exist");
}
}
}
with the following directory structure:
cFolder1
-->cFolder2
-->cFolder3
|>MyClass.java
folder1
-->folder2
|>myFile.txt
However, the above code is always giving the output:
The file does not exist
which is unexpected(to me). How can I solve this problem?
I have tried solutions as given in these QA links, but NONE worked for me: