當前位置:才華齋>計算機>java語言>

Java讀取、寫入檔案如何解決亂碼問題

java語言 閱讀(2.29W)

在我們讀取檔案流時,經常會遇到亂碼的現象,那麼Java讀取、寫入檔案如何解決亂碼問題呢?下面小編為大家解答一下,希望能幫到大家!

Java讀取、寫入檔案如何解決亂碼問題

首先,我們要明確一點,文字檔案與二進位制檔案的概念與差異。

文字檔案是基於字元編碼的檔案,常見的編碼有ASCII編碼,UNICODE編碼、ANSI編碼等等。二進位制檔案是基於值編碼的檔案,你可以根據具體應用,指定某個值是什麼意思(這樣一個過程,可以看作是自定義編碼。)

因此可以看出文字檔案基本上是定長編碼的(也有非定長的編碼如UTF-8)。而二進位制檔案可看成是變長編碼的,因為是值編碼嘛,多少個位元代表一個值,完全由你決定。

對於二進位制檔案,是千萬不能使用字串的,因為字串預設初始化時會使用系統預設編碼,然而,二進位制檔案因為自定義編碼自然與固定格式的編碼會有所衝突,所以對於二進位制的檔案只能採用位元組流讀取、操作、寫入。

對於文字檔案,因為編碼固定,所以只要在讀取檔案之前,採用檔案自身的編碼格式解析檔案,然後獲取位元組,再然後,通過指定格式初始化字串,那麼得到的文字是不會亂碼的'。雖然,二進位制檔案也可以獲取到它的文字編碼格式,但是那是不準確的,所以不能同日而語。

具體操作如下:

 1)獲取文字檔案的格式

public static String getFileEncode(String path) { String charset ="asci"; byte[] first3Bytes = new byte[3]; BufferedInputStream bis = null; try { boolean checked = false; bis = new BufferedInputStream(new FileInputStream(path)); (0); int read = (first3Bytes, 0, 3); if (read == -1) return charset; if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) { charset = "Unicode";//UTF-16LE checked = true; } else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) { charset = "Unicode";//UTF-16BE checked = true; } else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB && first3Bytes[2] == (byte) 0xBF) { charset = "UTF8"; checked = true; } t(); if (!checked) { int len = 0; int loc = 0; while ((read = ()) != -1) { loc++; if (read >= 0xF0) break; if (0x80 <= read && read <= 0xBF) //單獨出現BF以下的,也算是GBK break; if (0xC0 <= read && read <= 0xDF) { read = (); if (0x80 <= read && read <= 0xBF) //雙位元組 (0xC0 - 0xDF) (0x80 - 0xBF),也可能在GB編碼內 continue; else break; } else if (0xE0 <= read && read <= 0xEF) { //也有可能出錯,但是機率較小 read = (); if (0x80 <= read && read <= 0xBF) { read = (); if (0x80 <= read && read <= 0xBF) { charset = "UTF-8"; break; } else break; } else break; } } //ogger()(loc + " " + xString(read)); } } catch (Exception e) { tStackTrace(); } finally { if (bis != null) { try { e(); } catch (IOException ex) { } } } return charset; } private static String getEncode(int flag1, int flag2, int flag3) { String encode=""; // txt檔案的開頭會多出幾個位元組,分別是FF、FE(Unicode), // FE、FF(Unicode big endian),EF、BB、BF(UTF-8) if (flag1 == 255 && flag2 == 254) { encode="Unicode"; } else if (flag1 == 254 && flag2 == 255) { encode="UTF-16"; } else if (flag1 == 239 && flag2 == 187 && flag3 == 191) { encode="UTF8"; } else { encode="asci";// ASCII碼 } return encode; }

 2)通過檔案的編碼格式讀取檔案流

/** * 通過路徑獲取檔案的內容,這個方法因為用到了字串作為載體,為了正確讀取檔案(不亂碼),只能讀取文字檔案,安全方法! */ public static String readFile(String path){ String data = null; // 判斷檔案是否存在 File file = new File(path); if(!ts()){ return data; } // 獲取檔案編碼格式 String code = ileEncode(path); InputStreamReader isr = null; try{ // 根據編碼格式解析檔案 if("asci"ls(code)){ // 這裡採用GBK編碼,而不用環境編碼格式,因為環境預設編碼不等於作業系統編碼 // code = roperty("ding"); code = "GBK"; } isr = new InputStreamReader(new FileInputStream(file),code); // 讀取檔案內容 int length = -1 ; char[] buffer = new char[1024]; StringBuffer sb = new StringBuffer(); while((length = (buffer, 0, 1024) ) != -1){ nd(buffer,0,length); } data = new String(sb); }catch(Exception e){ tStackTrace(); ("getFile IO Exception:"+essage()); }finally{ try { if(isr != null){ e(); } } catch (IOException e) { tStackTrace(); ("getFile IO Exception:"+essage()); } } return data; }

 3)通過檔案指定的格式寫入檔案

/** * 按照指定的路徑和編碼格式儲存檔案內容,這個方法因為用到了字串作為載體,為了正確寫入檔案(不亂碼),只能寫入文字內容,安全方法 * * @param data * 將要寫入到檔案中的位元組資料 * @param path * 檔案路徑,包含檔名 * @return boolean * 當寫入完畢時返回true; */ public static boolean writeFile(byte data[], String path , String code){ boolean flag = true; OutputStreamWriter osw = null; try{ File file = new File(path); if(!ts()){ file = new File(arent()); if(!ts()){ rs(); } } if("asci"ls(code)){ code = "GBK"; } osw = new OutputStreamWriter(new FileOutputStream(path),code); e(new String(data,code)); h(); }catch(Exception e){ tStackTrace(); ("toFile IO Exception:"+essage()); flag = false; }finally{ try{ if(osw != null){ e(); } }catch(IOException e){ tStackTrace(); ("toFile IO Exception:"+essage()); flag = false; } } return flag; }

4)對於二進位制檔案而且內容很少的,例如Word文件等,可以使用如下方式讀取、寫入檔案

/** * 從指定路徑讀取檔案到位元組陣列中,對於一些非文字格式的內容可以選用這個方法 * 457364578634785634534 * @param path * 檔案路徑,包含檔名 * @return byte[] * 檔案位元組陣列 * */ public static byte[] getFile(String path) throws IOException { FileInputStream stream=new FileInputStream(path); int size=lable(); byte data[]=new byte[size]; (data); e(); stream=null; return data; } /** * 把位元組內容寫入到對應的檔案,對於一些非文字的檔案可以採用這個方法。 * @param data * 將要寫入到檔案中的位元組資料 * @param path * 檔案路徑,包含檔名 * @return boolean isOK 當寫入完畢時返回true; * @throws Exception */ public static boolean toFile(byte data[], String path) throws Exception { FileOutputStream out=new FileOutputStream(path); e(data); h(); e(); out=null; return true; }