-1

My project has two folders: folder1 and folder2. In folder1, I am trying to load a file present in folder2 using its reference path but it gives me the following exception: java.io.FileNotFoundException: /folder2/blah.txt (No such file or directory). It works when I use the absolute path.

file = new FileInputStream("/folder2/blah.txt")
Ava
  • 5,783
  • 27
  • 58
  • 86

2 Answers2

1

You should always call the paths by calling the root path of the application. I am assuming folder1 and folder2 are not related, meaning, folder2 is not in folder1 and thats why it is happening.

Please check How to get the real path of Java application at runtime? to get the root path then you can access the directories as you please

In other means you can try:

String path = new File(".").getCanonicalPath();
file = new FileInputStream( path + "/folder2/blah.txt"); // I have not tested here. If it returns an error try removing the / from the beginning of the folder2
Community
  • 1
  • 1
Hozikimaru
  • 1,144
  • 1
  • 9
  • 20
-1

It is a pretty simple fix. You are trying to load the file in your system at /folder2/blah.properties. However, if it is just in your Java project's classpath the initial File path separator is omitted.

Try something like this:

//Initial seperator is oitted.
file = new FileInputStream("folder2/blah.txt")
Ava
  • 5,783
  • 27
  • 58
  • 86
schmidt73
  • 181
  • 11