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