File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Store() Unicode in Properties file

 
Yash Sharma
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the stor() method of Properties class, which takes and Output stream as argument. How can I set the encoding of the stream so that the Unicode text is written properly. We can specify the encoding for a OutputStreamWriter in its contructor but how to do tat for an OutputStream?
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you could wrap the FileOutputStream in a PrintStream, which has a constructor that takes an OutputStream, a boolean for autoflush, and String for encoding.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the stor() method of Properties class, which takes and Output stream as argument. How can I set the encoding of the stream so that the Unicode text is written properly.
You can't if you use the Properties store() method. This method specifies its own format, which uses special escape sequeces for many chars, as described in the API. It doesn't matter what type of OutputStream you pass the store method; it's still going to use this escape format. If you want text files which allow non-European characters to be displayed properly to someone looking at the raw text, you'll need to use some other techniques to store and read the file. XML might be a goood option, as this allows you to choose an encoding, and put almost any type of chars in as values, provided they can be represented in the chosen encoding. Or you can make your own file format which could be much simpler - but you need to consider, can the values you're storing include newlines and carriage returns? Can they include '=' (or whatever other char you might want to try to use as a delimiter between key and value)? If the values to be stored do not have some restrictions on them, then you end up neededing to create special escape sequences; it can be a pain to deal with all the complications. Using a standard solution like XML is probably easier.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learn something new every day!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Properties class was around in JDK 1.0, while things like Readers and Writers weren't around until 1.1. So they made their own format, not integrated into the way we later learned to handle encodings. But hey, at least it works. Many other I/O classes in 1.0 had methods that had to be deprecated because they didn't handle encoding properly.
 
Yash Sharma
Ranch Hand
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys! A solution that I arrived at was storing the Unicode text in "/u" format without setting any encoding (very much the same way one would use the native2ascii tool to convert the files to).
Another problem:
I have however another problem. I have a web-interface to edit the property files. Through this JSP the user first selects a property file, I then display all the kesy for this file with checkboxes, user can select the key(s) and then change the values for these if he so wishes.
I am able to save the changes rightly to the property file. However when I select the same key for the same file again the old property shows up. I have to restart the application to get the changed property.
I have disallowed caching of my JSP. I religiously set all objects (including the property file objects) to null in the JSP everytime. How come the property file object is still being cached?
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This probably would be a good question to ask in the JSP forum, Yash. I would also supply some of the JSP code; it makes it easier to determine what might going wrong.
If it doesn't appear to be a JSP problem, we can address it here again.
[ December 12, 2003: Message edited by: jason adam ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic