• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Properties.load() ignores '\'

 
Yaroslav Chinskiy
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a properties file that contains a path in the form c:\test\tmp

If I use standard Properties.load method, it will strip all '\'

I can not modify the config file and must process as is.

Any advice?
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm no I/O guru, so there may be a better way of doing this, but you could write a custom FilterInputStream to convert the black slashes into forward slashes since Java will take C:/test/myfile.txt as a vaild file path for windows. (It would also take C:\\test\\myfile.txt). However to add the extra escape character would take a little bit more work since you need to add a character to the input stream, likely via a BufferedInputStream or BufferedReader.

Here's a quick trial I did that seems to work fine (with very limited testing), converting c:\test\tmp
to c:/test/tmp. Of course this will convert ALL '\' characters to '/'. Not a problem if you only have file paths in your properties file, If not, you'll to make it a bit more fancy, perhaps changing all '\' to '\\', but like I said, that gets to be a bit more complicated, but certainly could be done via some type of BufferedReader or Stream.



A demo program:


A sheriff may want to move this to the I/O forum to get some input (no pun intended) from the gurus that hang out in that forum.
[ March 15, 2005: Message edited by: Mark Vedder ]
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not use File operations to load your properties.
.Here's an excellent article that tells why and also details the correct approach (Use ClassLoader and Class apis)

ram
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a good point, and I agree it is generally better to use a ClassLoader then a a java.io.File to load the properties. I typically do so myself. In the above post, my focus was on the FilterInputStream to solve Yaroslav's problem, and therefore I wrote a quick demo program to illustrate its use. The same technique could be used on the wrap the InputStream received via using a ClassLoader to "find" the properties file.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic