8

So I am having a strange issue with Java.

I'm reading a writing files, so the path is important to me. I would like all files to be written and read from the relative path (i.e in the folder with the rest of my class and java files).

I write files like so:

FileWriter fw = new FileWriter(outfile,true);
fw.write(data);
fw.close();

where outfile is something like 'out.txt' (i.e. the name of the file we want the output to go in).

The problem is, the file is created in /home/me/ instead of the path of my other files.

Am i doing something wrong? Or how can i get the files stored in the relative directory?

Blackbinary
  • 3,936
  • 18
  • 49
  • 62
  • 1
    Time? http://en.wikipedia.org/wiki/Special_relativity – John Vint Nov 17 '10 at 23:26
  • Maybe relative is the wrong term. I mean in my working directory (i.e. where the files was built, where its being executed from etc.) – Blackbinary Nov 17 '10 at 23:26
  • 1
    In Java that's kind of a dicey concept, since in some environments there is no "current directory" (i.e. a WAR file in a web app). Also there's no guarantee that the "current directory" is the one your source or .class file is in, either. You have to define more precisely what you want to do. – Jim Garrison Nov 17 '10 at 23:28

4 Answers4

7
FileWriter fw = new FileWriter("./" + outfile,true);

or

FileWriter fw = new FileWriter("~/" + outfile,true);

I would try that.

Eric K Yung
  • 1,754
  • 11
  • 10
5

The working directory is where you run the program from i.e. where you call java YourClass, so when you want to write files relative to the class that's executing you'll have to know the current directory and append the relative path from there e.g.

So if you have a class test.YourClass and you run the application from the source root i.e

java test.YourClass

you can find the absolute path to the directory your class file is in from the the user.dir system property and the classes package name as follows:

public class YourClass {

public void printPath() {
    String path = String.format("%s/%s", System.getProperty("user.dir"), this.getClass().getPackage().getName().replace(".", "/"));
    System.out.println(path);
}

}

Ellis
  • 689
  • 1
  • 11
  • 24
3

Java will save files relative to the current working directory. Are you inside you home directory when you run the program?

If you always want files to be saved relative to the location of the Java files and not the current working directory, you need to find that directory and prepend it to get an absolute path.

Marcus Whybrow
  • 19,578
  • 9
  • 70
  • 90
  • I guess that was the problem. I was compiling all my files by the folder (so i was in my home directory instead of a step inside the folder), and i used package.AClass to run it. So i guess that answers that. Thanks. – Blackbinary Nov 17 '10 at 23:31
0

set the user.dir property on the command line, or just run from the directory you wnt everything to be relative to (by cd-ing)

oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449