简体   繁体   English

使用UTF-8字符读写属性文件

[英]Read and write property files with UTF-8 characters

I have a property file ,it contains UTF-8 charecters.(Please have a look at the sample one i pasted bellow.) 我有一个属性文件,它包含UTF-8字符。(请看一下我粘贴的样本。)

#-------------------------------------------------------------------------------
# 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. 这是我使用的代码,用于从特定的loacation读取一个propery文件并将内容写入其中。

 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");
    }

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. 您正在使用错误的store()方法 - 第二个参数不是要使用的编码(它将使用ISO-8859-1传统的属性文件编码,因此Unicode转义)但是注释。

The correct method takes a Writer as parameter. 正确的方法将Writer作为参数。 This should work: 这应该工作:

p.store(new OutputStreamWriter(
    new FileOutputStream(filePath), "UTF-8"), 
    comment);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM