2014-08-29 79 views
0

我正在閱讀包含€歐元符號的屬性文件。當它在屏幕上打印時,看起來完全不同。我比較了來自道具文件的字符串,並通過使用equals方法聲明另一個具有相同文本的字符串,它是false.Pls可以有人幫助我。歐元符號顯示不正確來自適當的文件

properities file 
your purchase order is € 

string text="your purchase order is €"; 

on comparing the above strings it fails. 

************************ 
public String getProperty(String arg0) { 
     Properties prop = new Properties(); 
     InputStream input = null; 


      try { 
       input = new FileInputStream("C:/text.properties"); 

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      try { 

       prop.load(input); 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     return prop.getProperty(arg0); 

    } 
+1

它是如何顯示的? – 2014-08-29 15:59:10

+0

你在哪裏輸出它? IDE控制檯?貝殼 ? – ortis 2014-08-29 16:06:15

回答

4

Properties.load方法,其採用InputStream假定該文件是保存在ISO-8859-1字符編碼,這是不能直接表示歐元符號。如果文件真的採用不同的編碼方式,例如UTF-8,那麼您應該使用load方法代替Reader,並使用InputStreamReader指定正確的編碼。

另外,Properties文件支持Unicode轉義序列,這樣你就可以代表歐元符號在文件中\u20ac和加載文件時,它會被解碼成一個真正的字符。

除此之外,您當前的代碼中還存在一些缺陷,最重要的是您需要確保輸入流在您從中加載屬性後正確關閉。要做到這一點最簡單的方法就是「嘗試與資源」語法

try(InputStream in = new FileInputStream("C:/text.properties"); 
    InputStreamReader reader = new InputStreamReader(in, "UTF-8")) { 
    prop.load(reader); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

而且似乎浪費重新裝入特性文件,你從它要求的值每一次,你可能會考慮加載它只有一次(無論是在前面還是第一次請求時),並緩存對象以備後用。