[英]Euro Symbol not displayed proper from properites file
我正在讀取包含€歐元符號的屬性文件。 當它在屏幕上打印時看起來完全不同。 我比較了props文件中的字符串,並使用equals方法聲明了另一個具有相同文本的字符串,這是錯誤的。請有人幫我。
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);
}
使用InputStream
的Properties.load
方法假定文件以ISO-8859-1
字符編碼保存,該字符編碼不能直接表示歐元符號。 如果文件確實采用不同的編碼(例如UTF-8),則應使用采用Reader
的load
方法,並使用InputStreamReader
指定正確的編碼。
另外, Properties
文件支持Unicode轉義序列,因此您可以在文件中將歐元符號表示為\€
,並且在加載文件時會將其解碼為真實的€
字符。
除此之外,當前代碼中還存在許多缺陷,最重要的是,您需要確保在從輸入流加載屬性后正確關閉了輸入流。 最簡單的方法是“嘗試資源”語法
try(InputStream in = new FileInputStream("C:/text.properties");
InputStreamReader reader = new InputStreamReader(in, "UTF-8")) {
prop.load(reader);
} catch (IOException e) {
e.printStackTrace();
}
每次您從屬性文件中請求值時重新加載屬性文件似乎都是浪費的,您可以考慮僅將其加載一次(預先或首次請求)並緩存Properties
對象以備后用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.