aspose file tools*
The moose likes I/O and Streams and the fly likes Store() Unicode in Properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Store() Unicode in Properties file" Watch "Store() Unicode in Properties file" New topic
Author

Store() Unicode in Properties file

Yash Sharma
Ranch Hand

Joined: Jan 30, 2003
Posts: 61
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

Joined: May 08, 2001
Posts: 1932
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Learn something new every day!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jan 30, 2003
Posts: 61
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

Joined: May 08, 2001
Posts: 1932
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Store() Unicode in Properties file