PrintWriter problems

hi

Im trying to save an int[] to a file, where each int is printed to a new line in a file. When the program is run from eclipse and is not jarred the file is saved to the parent map even when I havent specified any special map.

I use a printwriter like this:

PrintWriter(new FileWriter(filename+fileEnding), true);

I have tried to use “/filename.txt” but that doesnt change anything. Also when I try to load the file it works, but when i put the program in a jar it saves the file outside the jar,and it should, but it cant load the file because it doesnt find it.

What is the correct syntax for specifying the directory that the jar file is in?

This is what I use to read in the file line by line

new BufferedReader(new                       InputStreamReader(getClass().getResourceAsStream("./"+mapname+fileEnding)));

Should I use anything else than InputStreamReader maybe?

getResourceAsStream() searches for the file in the class context of the class for which it is called. Thus, if the class is inside a JAR, the file will be searched inside the JAR as well.

Therefore, do not use getResourceAsStream() for things such as INI files, where the file must be somewhere in the file system, out of the INI file.

Alternatively, you could use the java.util.prefs package, which is a platform-independent mechanism to store and load systems and user settings.

This is true but a bit incomplete.

It will actually look in its parent class loaders’ environment first, just as it does for classes. (In fact these calla are used to find the bits for classes.)

Asd the root of all class loader parental lists should be the boot class loader, follwoed by the classpath loader, it will find anything that is in your classpath.