java读取properties文件中文乱码的解决方法 收藏
java读取properties文件时,如果包含中文,那么该中文字段读出为乱码。这是因为java中文件大多以UTF-8或GBK的方式保存,而java程序在读出properties文件时则采用unicode编码方式,这样自然会导致中文乱码情况的发生。
spring怎么读取properties
这里,先重现一下该问题,然后给出解决方法。
读取properties的方法如下:
view plaincopy to clipboardprint?
public class TestPorperty { 
 
    private InputStream is;//用于读取(.properties)文件 
    private Properties prop; 
     
    private final String propPath="D:\\Documents and Settings\\Administrator\\桌面\\test.properties"; 
     
    public TestPorperty() throws Exception{ 
        prop=new Properties(); 
    } 
    public String getProperties(String key) throws Exception{ 
        is=new FileInputStream(propPath); 
        prop.load(is); 
        Property(key); 
    } 
public class TestPorperty {
    private InputStream is;//用于读取(.properties)文件
    private Properties prop;
   
    private final String propPath="D:\\Documents and Settings\\Administrator\\桌面\\test.properties";
   
    public TestPorperty() throws Exception{
        prop=new Properties();
    }
    public String getProperties(String key) throws Exception{
        is=new FileInputStream(propPath);
        prop.load(is);
        Property(key);
    }
}
源文件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 stGetProperties(TestPorpertyTest.java:25) 
    flect.NativeMethodAccessorImpl.invoke0(Native Method) 
    flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at flect.Method.invoke(Method.java:597) 
    at org.junit.internal.uteMethodBody(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) 
    lipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.jav
a:38) 
    lipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) 
    lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) 
    lipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) 
    lipse.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 stGetProperties(TestPorpertyTest.java:25)
    flect.NativeMethodAccessorImpl.invoke0(Native Method)
    flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。