• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Properties.load() ignores '\'

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic