Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Properties, getting file path

logpath = LoggerUtils.getProperties().getProperty("log.path");
System.out.println("logpath: " + logpath);

The above code returns:

logpath: C:UsersMauriceDesktopLogs

In the properties file is:

log.path    C:\Users\Maurice\Desktop\Logs

How do I retain the file separators? I want this to work on Linux as well and not just Windows.

like image 611
Maurice Avatar asked Feb 21 '12 02:02

Maurice


People also ask

What is the path of properties file in Java?

Read properties file from classpath You have $project/src as default classpath as this src folder will be copied to classes. You can put it in $project/src folder and read it from there.

How do I give a file path to a properties file?

when loading the properties from the file with the method Properties. load(), I need to use the escape character '\' in the path. So my path looks like: C:\\Users\\Harald\\Folder1\\Version1\\Folder2 . And it works this way, no exception is thrown.

How do I find the path of a file in Java?

In Java, for NIO Path, we can use path. toAbsolutePath() to get the file path; For legacy IO File, we can use file. getAbsolutePath() to get the file path.


1 Answers

Actually, you need to put this in the property file:

log.path    C:\\Users\\Maurice\\Desktop\\Logs

See this:

  • http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

more precisely the load method:

  • http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)

Scroll down a bit and you will see this among other things:

The method does not treat a backslash character, \, before a non-valid escape character as an error; the backslash is silently dropped. For example, in a Java string the sequence "\z" would cause a compile time error. In contrast, this method silently drops the backslash. Therefore, this method treats the two character sequence "\b" as equivalent to the single character 'b'.

Backslash \ is an escape character that is silently dropped otherwise.

like image 136
icyrock.com Avatar answered Oct 10 '22 02:10

icyrock.com