2016-05-25 26 views
-4

我有一個字符串,它是通過一個很長的過程decoded/encoded/encrypted(涉及file writing/reading)。爲什麼我在Java String中有?

在解碼後,我的「明文」字符串開頭我有兩個奇怪的字符:

這是什麼?爲什麼在那裏?如何擺脫它?

謝謝。

編輯:

這是我寫的一個文件:

try { 
    FileOutputStream out = new FileOutputStream(filePath); 
    out.write(string.getBytes()); 
    out.close(); 
} catch (Exception e) { 
    //handle exception 
} 
+0

我認爲這是一個字節順序標記(BOM)http://stackoverflow.com/questions/2223882/whats-different-between-utf-8-and-utf-8-without-bom – Stewart

+2

這可能是一個[BOM](https://en.wikipedia.org/wiki/Byte_order_mark),這是在你的文件寫作中創建的。顯示你的代碼。 – Kayaman

+0

請務必在提問前閱讀此內容:http://stackoverflow.com/help/how-to-ask – tak3shi

回答

1

我幾乎可以保證這是一個編碼問題

  1. 切勿使用new String(byte[])(總是傳遞一個字符集)
  2. 永不使用new InputStreamReader(inputStream)(總是通過字符集)
  3. 切勿使用String.getBytes()(總是傳遞一個字符集)
  4. 確保編輯器(如記事本)使用相同的編碼被讀取的文件被寫入

如果不明確地傳遞一個字符集,默認將被選中。在java中,這對於UNIX/Windows將是不同的(除非你明確地設置了file.encoding系統屬性)。

相關問題