• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to a Property File  RSS feed

 
sheril she
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to write other characters ie EUC-JP (for japanese)characters to the property file.
this is the code that i have written.in the jsp page & servlet which calls this code i have set the request.setCharacterEncoding("EUC-JP").but it is still saving as unicode characters in the file
FileInputStream fp1 = new FileInputStream(PROPERTYFILE);
prs.load(fp1);
for(int i=0;i<labels.length;i++)
{
prs.setProperty(labels,values);
}
FileOutputStream op1 = new FileOutputStream(PROPERTYFILE);
prs.save(op1,"");
fp1.close();

Can anyone pls help
 
Eddie Sheffield
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. Well this is sort of a guess, but you might try this:
-----------------
FileInputStream fp1 = new FileInputStream(PROPERTYFILE);
prs.load(fp1);
fp1.close();
for(int i=0;i<labels.length;i++)
{
prs.setProperty(labels,values);
}
FileOutputStream op1 = new FileOutputStream(PROPERTYFILE);
PrintStream ps1 = new PrintStream(op1, true, "EUC-JP");
prs.store(ps1,"");
ps1.close();
op1.close();
------------------
Couple of things to consider. Your VM has to support the desired character encoding. The javadocs say to check the release notes for your VM to see what your specific release includes. I also changed the "save" to "store" as Properties.save() is deprecated. But Properties.store() throws an IOException.
You didn't mention which JDK version you're using, but if you have JDK 1.4 and your JVM doesn't have the character encoding support you need, you might have to look into using the java.nio.charset and java.nio.charset.spi packages and providing your own encoding provider. Or perhaps you can find a 3rd party provider to suit your needs.
Hope this helps!
 
sheril she
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eddie,
Thanx for the effort.But the PrintStream does not take the constructor with this format(op1, true, "EUC-JP")
Encoding is supported only in the OutputStreamWriter and InputStreamReader classes.
i tried that too.but the store/save method does not accept these objects.
we r using jdk1.3.1,tomcat 4.1.12,servlet 2.3 and encoding is not a problem.
from the jsp,servlet pages it is writing euc characters perfectly to the
database but only writing to property file is not working.
The entire file is converted to unicode characters.
we r maintaining this file for translation to japanese.
do i need to use the PropertyResourceBundle?I have no idea about it.
This is very urgent.Pls help.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sheril,

Is it possible to write other characters ie EUC-JP (for japanese)characters to the property file.

Not with java.util.Properties. I don't know what version of SDK you are using, but Properties.save() has been deprecated since 1.2. Since then save() just calls store() which is the preferred method. Properties.store() only uses ISO 8859-1 character encoding. If you want to use a different encoding you'll need to subclass Properties and override the store method and use an OutputStreamWriter with EUC-JP encoding. You will also undoubtedly need to override the load() method as well, to translate back to ISO 8859-1 before passing the stream back to super.load().
Michael Morris
[ April 04, 2003: Message edited by: Michael Morris ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheril, you say you're using SDK 1.3.1. If you can switch to 1.4, you can also use the Preferences class as a more powerful and flexible alternative to Properties. One benefit is that there's an exportSubtree() method which uses UTF-8, which is at least able to encode all the characters you need (unlike the ISO-9959-1 used by Properies). However if you really need the file to use EUC-JP, and/or you can't switch to 1.4, then your best bet is to forget about the store() and load() methods of Properties, and write your own methods to write and read properties to/from a file using EUC-JP. It's really not difficult:

[ April 04, 2003: Message edited by: Jim Yingst ]
 
sheril she
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx a lot Jim!This code has worked.
And Thanx to Michael and Eddie too!
Regards
 
sheril she
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we should be using entry.getKey() instead of getName().
One more small problem.
The order of lines in the property file is getting changed completely.Is there a way to maintain this order?
Regards
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not while using the Properties class. Like Hashtable and HashMap, it stores things in an order determined by the hashCode() value of the keys. Which for your purposes is like using random numbers - the hash codes have nothing to do with the order you want the objects to be in.
Probably your best bet is to replace the Properties with a LinkedHashMap. This is basically a hash map that also stores additional data to maintain an internal order. You can use it much like the Properties object, using get() instead of getProperty(), and inserting a cast to (String) when the compiler tells you to.
 
sheril she
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Jim!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!