Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use relative path instead of absolute path?

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?

like image 778
Blackbinary Avatar asked Nov 17 '10 23:11

Blackbinary


People also ask

Why is it better to use relative paths instead of absolute paths?

Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.

When would you use a relative path vs an absolute path?

In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.

How do you convert relative path to absolute path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.


3 Answers

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

or

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

I would try that.

like image 158
Eric.K.Yung Avatar answered Nov 09 '22 14:11

Eric.K.Yung


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);
}

}

like image 24
Ellis Avatar answered Nov 09 '22 15:11

Ellis


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.

like image 32
Marcus Whybrow Avatar answered Nov 09 '22 13:11

Marcus Whybrow