简体   繁体   中英

How to specify a path in a properties file to be loaded with Java Properties

I have a.properties file I want to load in a Java Properties object. The file contains some paths. In order to be able to load the properties file without getting a java.lang.IllegalArgumentException: Malformed \uxxxx encoding. 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.

The problem is that I can't explain to my user (because this path can be changed by the user.) that he needs to use double '\' in the path.

Does anybody knows a work-around for this problem? Thanks!

Even simpler solution is to use forward slashes . This way your path will look like C:/Users/Harald/Folder1/Version1/Folder2 and everything else will just work - no code change required!

I am a bit confused with your question. Are you asking if there is a way that you can have a user edit the properties file without having to use non-intuitive \\ escape character?

If you are setting this up for a user to be able to edit perhaps you should have a program or script to create the properties file (adding in escape characters when needed)

You might be better off defining a path for the user to place a properties file, or point them to an existing properties file (that contains comments about useful properties etc.) eg ${user.home}\\\\myapp\\\\myprops.properties and then always load that file, or better yet, just provide the directory and load every file that ends in .properties in that directory.

If your application is a GUI app, you could also allow the user to select a file in a file dialog box. The strings you get back out of swing and SWT labels and file choosers are java friendly and you needn't worry about escape characters.

if in java code use \\

if in the properties file use /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM