Accessing file with FileInputStream from a different directory

Hi

Could someone tell me how can I read a file say "map.txt" from a directory other than the one the project is in, please ?

my directory structure is

"Map" directory has 2 folders insise:

-> Folder1 (where is my project) and

-> Folder2 (where is "map.txt")

and I have :

FileInputStream fos = new FileInputStream("/Folder2/map.txt");

but it doesn't work

I hope you get what I mean.

cheers

[466 byte] By [qmalqa] at [2007-11-26 17:42:36]
# 1
Just use the full path to the file!
sabre150a at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks but anyway i could use a relative path ?
qmalqa at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 3
Anything stopping you from trying it and seeing if it works?
DrLaszloJamfa at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 4
I've tried the full path, it works, but the relative one doesn't .
qmalqa at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 5

Relative paths only work if both you and the system agree on what the current directory is. That's why they are a bit fragile. Try this:

public class Example {

public static void main(String[] args) {

System.out.println(System.getProperty("user.dir"));

}

}

DrLaszloJamfa at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 6
If you are certain you are in the correct directory then try placing a dot in front of the path.File f = new File("./Dir/File");and no there is no need for people to hound me about my previous advice of using dot. :p
floundera at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 7

> If you are certain you are in the correct directory

> then try placing a dot in front of the path.

>

> > File f = new File("./Dir/File");

>

Remove both the dot and the initial / from the above. The initial / is the real problem here. OP is telling it to find Dir in the root directory, and he already said that it's in the Map directory. So either he has to be in Map and use Dir/File, or he has to provide a complete path to Map/Dir/File, or he has to do something with ..'s.

ejpa at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 8
Yeah. The safest bet is to use the full path. Then you are guaranteed to find the file unless someone moves it.
floundera at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 9
> Yeah. The safest bet is to use the full path. Then> you are guaranteed to find the file unless someone> moves it.YAY! 3,000!
CaptainMorgan08a at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 10
> YAY! 3,000![does happy dance]Thanks! Mind you I'd have more if I was a Duke chaser. ;)
floundera at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...
# 11
> Thanks! Mind you I'd have more if I was a Duke> chaser. ;)Are you implying something? ;-)
CaptainMorgan08a at 2007-7-9 0:10:48 > top of Java-index,Java Essentials,Java Programming...