Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a relative path in java

Here is the structure of my project :

here is the structure of my project

I need to read config.properties inside MyClass.java. I tried to do so with a relative path as follows :

// Code called from MyClass.java File f1 = new File("..\\..\\..\\config.properties");   String path = f1.getPath();  prop.load(new FileInputStream(path)); 

This gives me the following error :

..\..\..\config.properties (The system cannot find the file specified) 

How can I define a relative path in Java? I'm using jdk 1.6 and working on windows.

like image 532
ishk Avatar asked Jan 08 '13 06:01

ishk


People also ask

How do you define a relative path?

A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.

What is a relative path Example?

A relative path is a way to specify the location of a directory relative to another directory. For example, suppose your documents are in C:\Sample\Documents and your index is in C:\Sample\Index. The absolute path for the documents would be C:\Sample\Documents.

How do you find the relative path?

GetRelativePath which can be used like so: var relativePath = Path. GetRelativePath( @"C:\Program Files\Dummy Folder\MyProgram", @"C:\Program Files\Dummy Folder\MyProgram\Data\datafile1. dat");


2 Answers

Try something like this

String filePath = new File("").getAbsolutePath(); filePath.concat("path to the property file"); 

So your new file points to the path where it is created, usually your project home folder.

[EDIT]

As @cmc said,

    String basePath = new File("").getAbsolutePath();     System.out.println(basePath);      String path = new File("src/main/resources/conf.properties")                                                            .getAbsolutePath();     System.out.println(path); 

Both give the same value.

like image 51
Vinay Veluri Avatar answered Sep 27 '22 21:09

Vinay Veluri


Firstly, see the different between absolute path and relative path here:

An absolute path always contains the root element and the complete directory list required to locate the file.

Alternatively, a relative path needs to be combined with another path in order to access a file.

In constructor File(String pathname), Javadoc's File class said that

A pathname, whether abstract or in string form, may be either absolute or relative.

If you want to get relative path, you must be define the path from the current working directory to file or directory.Try to use system properties to get this.As the pictures that you drew:

String localDir = System.getProperty("user.dir"); File file = new File(localDir + "\\config.properties"); 

Moreover, you should try to avoid using similar ".", "../", "/", and other similar relative to the file location relative path, because when files are moved, it is harder to handle.

like image 42
hoapham Avatar answered Sep 27 '22 21:09

hoapham