Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read and write property files with UTF-8 characters

Tags:

java

utf-8

I have a property file ,it contains UTF-8 charecters.(Please have a look at the sample one i pasted bellow.)

#-------------------------------------------------------------------------------
# Resource strings for Root SIP
#-------------------------------------------------------------------------------
# @TRNSLATION-NOTES/* 
#                   * Don't translate the information in META-DATA-SEGMENT
#                   */@TRNSLATION-NOTES
# @META-DATA-SEGMENT/*
# Resource file related information (metadata)
#---
root.label.hpcards=HP képeslapok
root.label.128.2=HP barkácsolás
root.label.weather=Időjárás
root.label.401.2=Tête à modeler
#---
# Locale Information
#---
resource.meta.lang=ar
# */@META-DATA-SEGMENT
#---
# Labels
#---
# @TRNSLATION-NOTES/* 2010-08-11: Following are added. To be translated.
#                   */@TRNSLATION-NOTES
root.label.getmore=الحصول على المزيد
root.msg.apps=التطبيقات
root.msg.getmore=الرجاء زيارة الموقع www.hpeprintcenter.com لمعرفة المزيد حول التطبيقات وإدارة التطبيقات على هذه الطابعة. يختلف توفر التطبيق حسب البلد واللغة.
# @TRNSLATION-NOTES/* 2010-08-11: Above are added. To be translated.
#                   */@TRNSLATION-NOTES
root.label.2.2=قسيمات
root.label.35.2=MSNBC
root.label.43.2=PBS Kids
root.label.47.2=موجز Yahoo!
# @TRNSLATION-NOTES/* 2010-08-11: Following are added. To be translated.
#                   */@TRNSLATION-NOTES

root.label.dreamworks=DreamWorks
root.label.hpcards=البطاقات في HP
root.label.128.2=الأعمال اليدوية في HP
root.label.nickelodeon=Nickelodeon
root.label.quickforms=استمارات سريعة
root.label.recipes=وصفات الطعام
root.label.sudoku=سودوكو
root.label.tabbloid=Tabbloid
root.label.usatoday=USA Today
root.label.weather=حالة الطقس
#---
# Error Messages
#---
# @TRNSLATION-NOTES/* 1. The text before the first colon (i.e. Print Apps) is displayed as title in the screen
#                   */@TRNSLATION-NOTES
# @TRNSLATION-NOTES/* 2010-08-16: Following is to be translated.
#                   */@TRNSLATION-NOTES
root.msg.fault.project.perz=Print Apps: لم يتم تثبيت أي Apps على الطابعة لديك. يمكنك تثبيتApps على الطابعة باستخدام موقع Apps على ويب في عنوان URL التالي : www.hpeprintcenter.com
root.msg.fault.project.unexpected=تطبيقات الطباعة: غير متوفرة مؤقتًا. الرجاء المحاولة مرة أخرى لاحقًا.

I have to edit the contents of this property file.Editing process carrying out succesfully.But after editing the property file output is dispalyed as bellow.

#UTF-8
#Fri Dec 09 15:17:55 IST 2011
root.label.nickelodeon=Nickelodeon
root.label.2.2=\u0642\u0633\u064A\u0645\u0627\u062A
root.label.sudoku=\u0633\u0648\u062F\u0648\u0643\u0648
\uFEFF\#-------------------------------------------------------------------------------=
root.label.401.2=T\u00EAte \u00E0 modeler
root.msg.apps=\u0627\u0644\u062A\u0637\u0628\u064A\u0642\u0627\u062A
root.label.35.2=MSNBC
root.label.43.2=PBS Kids
root.msg.fault.project.perz=Print Apps\: \u0644\u0645 \u064A\u062A\u0645 \u062A\u062B\u0628\u064A\u062A \u0623\u064A Apps \u0639\u0644\u0649 \u0627\u0644\u0637\u0627\u0628\u0639\u0629 \u0644\u062F\u064A\u0643. \u064A\u0645\u0643\u0646\u0643 \u062A\u062B\u0628\u064A\u062AApps \u0639\u0644\u0649 \u0627\u0644\u0637\u0627\u0628\u0639\u0629 \u0628\u0627\u0633\u062A\u062E\u062F\u0627\u0645 \u0645\u0648\u0642\u0639 Apps \u0639\u0644\u0649 \u0648\u064A\u0628 \u0641\u064A \u0639\u0646\u0648\u0627\u0646 URL \u0627\u0644\u062A\u0627\u0644\u064A \: www.hpeprintcenter.com
root.label.getmore=\u0627\u0644\u062D\u0635\u0648\u0644 \u0639\u0644\u0649 \u0627\u0644\u0645\u0632\u064A\u062F
root.label.tabbloid=Tabbloid
root.label.128.2=\u0627\u0644\u0623\u0639\u0645\u0627\u0644 \u0627\u0644\u064A\u062F\u0648\u064A\u0629 \u0641\u064A HP
root.label.47.2=\u0645\u0648\u062C\u0632 Yahoo\!
root.label.86.3=MARMITON-1
root.label.hpcards=\u0627\u0644\u0628\u0637\u0627\u0642\u0627\u062A \u0641\u064A HP
root.label.weather=\u062D\u0627\u0644\u0629 \u0627\u0644\u0637\u0642\u0633
root.msg.fault.project.unexpected=\u062A\u0637\u0628\u064A\u0642\u0627\u062A \u0627\u0644\u0637\u0628\u0627\u0639\u0629\: \u063A\u064A\u0631 \u0645\u062A\u0648\u0641\u0631\u0629 \u0645\u0624\u0642\u062A\u064B\u0627. \u0627\u0644\u0631\u062C\u0627\u0621 \u0627\u0644\u0645\u062D\u0627\u0648\u0644\u0629 \u0645\u0631\u0629 \u0623\u062E\u0631\u0649 \u0644\u0627\u062D\u0642\u064B\u0627.
root.label.dreamworks=DreamWorks
resource.meta.lang=ar
root.label.quickforms=\u0627\u0633\u062A\u0645\u0627\u0631\u0627\u062A \u0633\u0631\u064A\u0639\u0629
root.label.recipes=\u0648\u0635\u0641\u0627\u062A \u0627\u0644\u0637\u0639\u0627\u0645
root.msg.getmore=\u0627\u0644\u0631\u062C\u0627\u0621 \u0632\u064A\u0627\u0631\u0629 \u0627\u0644\u0645\u0648\u0642\u0639 www.hpeprintcenter.com \u0644\u0645\u0639\u0631\u0641\u0629 \u0627\u0644\u0645\u0632\u064A\u062F \u062D\u0648\u0644 \u0627\u0644\u062A\u0637\u0628\u064A\u0642\u0627\u062A \u0648\u0625\u062F\u0627\u0631\u0629 \u0627\u0644\u062A\u0637\u0628\u064A\u0642\u0627\u062A \u0639\u0644\u0649 \u0647\u0630\u0647 \u0627\u0644\u0637\u0627\u0628\u0639\u0629. \u064A\u062E\u062A\u0644\u0641 \u062A\u0648\u0641\u0631 \u0627\u0644\u062A\u0637\u0628\u064A\u0642 \u062D\u0633\u0628 \u0627\u0644\u0628\u0644\u062F \u0648\u0627\u0644\u0644\u063A\u0629.
root.label.usatoday=USA Today

Here is the code i used, for read a propery file from specific loacation and write contents in to it.

 private static void saveFile(String filePath, String propertyStr, String appName) throws IOException {     
        BufferedReader fis = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"UTF-8"));  
        Properties p = new Properties();
        p.load(fis);    
        fis.close();
        p.setProperty(propertyStr, appName);
        p.store(new FileOutputStream(filePath), "UTF-8");
    }
like image 681
divz Avatar asked Dec 07 '22 17:12

divz


1 Answers

You're using the wrong store() method - the second parameter is not the encoding to use (it will use ISO-8859-1 the traditional Properties file encoding, hence the Unicode escapes) but a comment.

The correct method takes a Writer as parameter. This should work:

p.store(new OutputStreamWriter(
    new FileOutputStream(filePath), "UTF-8"), 
    comment);
like image 145
Michael Borgwardt Avatar answered Dec 09 '22 05:12

Michael Borgwardt