java读取properties文件中文乱码的解决方法

时间:2024.4.27

java读取properties文件中文乱码的解决方法 收藏

java读取properties文件时,如果包含中文,那么该中文字段读出为乱码。这是因为java中文件大多以UTF-8或GBK的方式保存,而java程序在读出properties文件时则采用unicode编码方式,这样自然会导致中文乱码情况的发生。

这里,先重现一下该问题,然后给出解决方法。

读取properties的方法如下:

view plaincopy to clipboardprint?

public class TestPorperty {

private InputStream is;//用于读取(.properties)文件

private Properties prop;

private final String propPath="D:\\Documents and

\\test.properties";

public TestPorperty() throws Exception{

prop=new Properties();

}

public String getProperties(String key) throws Exception{

is=new FileInputStream(propPath);

prop.load(is);

return prop.getProperty(key);

}

}

public class TestPorperty {

private InputStream is;//用于读取(.properties)文件

private Properties prop;

private final String propPath="D:\\Documents and

\\test.properties";

public TestPorperty() throws Exception{

prop=new Properties();

}

public String getProperties(String key) throws Exception{

is=new FileInputStream(propPath);

prop.load(is);

return prop.getProperty(key);

}

} Settings\\Administrator\\桌面 Settings\\Administrator\\桌面

源文件test.properties内容如下:

view plaincopy to clipboardprint?

name=测试

value=测试值

name=测试

value=测试值

测试用例如下(注:这里利用junit进行相关测试)

view plaincopy to clipboardprint?

@Test

public void testGetProperties() throws Exception{

TestPorperty tp=new TestPorperty();

Assert.assertEquals("测试", tp.getProperties("name"));

Assert.assertEquals("测试值", tp.getProperties("value"));

}

@Test

public void testGetProperties() throws Exception{

TestPorperty tp=new TestPorperty();

Assert.assertEquals("测试", tp.getProperties("name"));

Assert.assertEquals("测试值", tp.getProperties("value"));

}

执行该测试用例,结果如下:

view plaincopy to clipboardprint?

org.junit.ComparisonFailure: null expected:<[测试]> but was:<[????]>

at org.junit.Assert.assertEquals(Assert.java:92)

at org.junit.Assert.assertEquals(Assert.java:104)

at test.TestPorpertyTest.testGetProperties(TestPorpertyTest.java:25)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)

at

org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at

org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75)

at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)

at

org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)

at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at

org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)

at

org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at

org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at

org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) org.junit.ComparisonFailure: null expected:<[测试]> but was:<[????]>

at org.junit.Assert.assertEquals(Assert.java:92)

at org.junit.Assert.assertEquals(Assert.java:104)

at test.TestPorpertyTest.testGetProperties(TestPorpertyTest.java:25)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)

at

org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at

org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45)

at

org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:71)

at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at

org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)

at

org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at

org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at

org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

对于该问题,解决方法就是将properties文件中内容采用第三方工具转化为unicode编码的内容。

这里采用的是UltraEdit来实现这种转化。

首先,用UltraEdit新建一个文件,然后把java的properties文件中内容拷贝并粘贴到此新建文件中,接着保存该新建文件,并在Format下拉选择框中选择“unicode-ASCCII Escaped”方式保存,就会发现该文件中中文已被转化为unicode的了。这时再执行相关测试,就会发现乱码问题已经不存在了。

经过转换后的test.properties文件如下:

view plaincopy to clipboardprint?

name=\u6D4B\u8BD5

value=\u6D4B\u8BD5\u503C

name=\u6D4B\u8BD5

value=\u6D4B\u8BD5\u503C

执行上面测试,执行结果如下:


第二篇:java中文乱码的解决方法


方法一:可用于和spring集成的解决中文乱码问题

在web.xml中配置

方法二:

适用于和struts1集成的,解决中文乱码问题:

用actionServlet解决中文乱码问题

===============================

自定义一个Servlet 继承 apache的actionServlet

在自定义的servlet中的service的方法中添加一段代码!

---例如下面的自定义异常类

并在web.xml中从新注册actionServlet

用EncodingActionServlet替换了apache的actionServlet中心控制器因为自定义的servlet继承于actionServlet所以他可以替代actionServlet

方法三:适用于所有程序

用filter

更多相关推荐:
怎样写好research proposal(中文)

大多数学生和刚起步的研究者都不了解什么是研究计划也不知道其重要性简单的说一个人研究计划的好坏决定了其研究的好坏一个构思欠佳的研究计划会毁了整个项目即使它勉强通过了论文答辩委员会另一方面一个高质量的研究计划不仅确...

research proposal

DevelopingResearchProposalsHandoutPreparedbyTedZornUniversityofWaikatoThisisahandoutIoftengivetostudentswhenIexpect...

research proposal范文

Researchproposal1TitleCormacMcCarthysTheRoadandAmericanModernEschatologyOrModernEschatologyofthe21stcentu...

怎样写好research proposal

每个学术研究者必须经历的一道关卡就是ResearchProposal的写作它大致对应中文里的开题报告选题报告研究报告是一项研究开始之前的提纲规划和陈述既是为了帮助自己梳理文献整理思路廓清方向也常常是写给相关他人...

申请文书Research proposal怎么写

申请文书Researchproposal怎么写首先先讨论最常见的SOP无论申请什么学校一定都会要求SOP这份文件通常分以下几个部份大学时期修过课程大学时期研究硕班时期修过课程硕班时期研究课外活动打工或经验毕业后...

怎样写一篇好的research proposal

怎样写一篇好的researchproposal每个学术研究者必须经历的一道关卡就是ResearchProposal的写作它大致对应中文里的开题报告选题报告研究报告是一项研究开始之前的提纲规划和陈述既是为了帮助自...

CSC research proposal格式参考

国家建设高水平大学公派研究生项目研修计划ResearchPlanforCSCScholarshipProgram国家建设高水平大学公派研究生项目研修计划ResearchPlanforCSCScholarship...

format of a research paper introduction论文的标准格式

WritingaResearchPaperIntroductionAstepbystepreferenceTheintroductionisthefirstchapterofaItstartsfromtheresearchprop...

Paper Proposal0

PaperProposalEngineeringIntroductiontomechanicalEngineeringTABLEOFCONTENTSEXECUTIVESUMMARY错误未定义书签12INTROD...

提案报告写作Report and Technical Writing-Proposal

ReportandTechnicalWritingProposalsICheckingtheExercisesofAnalyticalReports1Whatarethemajordifferencesbetweeninforma...

论文架构 Research Paper

论文架构ResearchPaper1选题2查阅资料将选题具体化3收集资料并整理作笔记或卡片4开题报告5撰写任务书6提纲起草论文文献综述毕业论文文献综述文献综述要写明目的范围研究现状以及作者的评价和自己的观点一篇...

position paper-中文范文

代表学校国家委员会议题金融危机中的国际合作自20xx年9月以来由美国次贷危机所引发的金融经济危机已经波及全球在此期间如华尔街五大投资公司的彻底消失股市期货的一路贬值各国经济指标的自由落体式的下滑都不约而同地一再...

research proposal(23篇)