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.
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.
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.
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.
Actually, you need to put this in the property file:
log.path C:\\Users\\Maurice\\Desktop\\Logs
See this:
more precisely the load
method:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With