Reputation: 144
I'm working on a swing app that will allow users to view sets of (mostly locally-stored) files. I'm able to store lists of files (among other preferences) as text in Properties files but when I try to pull file values back out, using
myProperties.getProperty("File")
it discards some of the '\'s that are in the text. I confirmed that writing to the Properties files like this:
myPrintWriter.println("File = " + myFile.toString().replaceAll("\\\\", "/"))
or this:
myPrintWriter.println("File = " + myFile.toString().replaceAll("\\\\", "\\\\\\\\"))
would allow getProperty()
to work more like I expected but I didn't like having to convert my data to a Java-specific format without an explanation in the API so I decided to write it up on Stack Overflow.
But when I got to this point in my post started feeling embarrassed; I was about to post on Stack Overflow without even consulting the Java source code for java.util.Properties
(distributed with the JDK). I found that the Javadoc comments for Properties.load()
indicate this was a design decision which, for example, allows Properties values to contain line breaks. I guess I'll look into writing my Properties files in XML if I find that I need that language-independence.
Epilogue: So this is my first post on Stack Overflow and I've answered my own question but I've decided to post it anyhow in the hopes some person (or, perhaps, some artificial natural-language-processing-intelligence) may find it useful (or illuminating). Also, I'm curious as to any ideas or suggestions people might post in reply. TL;DR? Thanks for reading!
Upvotes: 3
Views: 1434
Reputation: 25400
You could use Properties.store()
to write out your properties. store()
is documented as writing properties in a format that load()
will read, so it will take care of any massaging that needs to be done.
Upvotes: 1
Reputation: 21223
If applicable, you can use double backslashes - c:\\test\\test.txt
:
Properties props = new Properties();
props.setProperty("test", "c:\\test\\test.txt");
System.out.println(props.getProperty("test"));
On Windows system you can also use forward slashes - c:/test/test.txt
:
Properties props = new Properties();
props.setProperty("test", "c:/test/test.txt");
System.out.println(props.getProperty("test"));
Upvotes: 2