首頁 > Java > Java基礎 > 主體

java讀取txt檔案亂碼解決方法

發布: 2019-11-30 09:38:32
原創
6762 人瀏覽過

java讀取txt檔案亂碼解決方法

java讀取txt文件,如果編碼格式不匹配,就會出現亂碼現象。所以讀取txt檔案的時候需要設定讀取編碼。 txt文檔編碼格式都是寫在文件頭的,在程式中需要先解析文件的編碼格式,取得編碼格式後,在以此格式讀取文件就不會產生亂碼了。 (推薦:java影片教學

java編碼與txt編碼對應:

java讀取txt檔案亂碼解決方法

範例:

package com.lfl.attachment;  
  
import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
  
public class TextMain {  
  
    public static void main(String[] args) throws Exception {  
        String filePath = "D:/article.txt";  
//      String filePath = "D:/article333.txt";    
//      String filePath = "D:/article111.txt";    
        String content = readTxt(filePath);  
        System.out.println(content);  
          
    }  
  
      
      
    /** 
     * 解析普通文本文件  流式文件 如txt 
     * @param path 
     * @return 
     */  
    @SuppressWarnings("unused")  
    public static String readTxt(String path){  
        StringBuilder content = new StringBuilder("");  
        try {  
            String code = resolveCode(path);  
            File file = new File(path);  
            InputStream is = new FileInputStream(file);  
            InputStreamReader isr = new InputStreamReader(is, code);  
            BufferedReader br = new BufferedReader(isr);  
//          char[] buf = new char[1024];  
//          int i = br.read(buf);  
//          String s= new String(buf);  
//          System.out.println(s);  
            String str = "";  
            while (null != (str = br.readLine())) {  
                content.append(str);  
            }  
            br.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.err.println("读取文件:" + path + "失败!");  
        }  
        return content.toString();  
    }  
      
      
      
    public static String resolveCode(String path) throws Exception {  
//      String filePath = "D:/article.txt"; //[-76, -85, -71]  ANSI  
//      String filePath = "D:/article111.txt";  //[-2, -1, 79] unicode big endian  
//      String filePath = "D:/article222.txt";  //[-1, -2, 32]  unicode  
//      String filePath = "D:/article333.txt";  //[-17, -69, -65] UTF-8  
        InputStream inputStream = new FileInputStream(path);    
        byte[] head = new byte[3];    
        inputStream.read(head);      
        String code = "gb2312";  //或GBK  
        if (head[0] == -1 && head[1] == -2 )    
            code = "UTF-16";    
        else if (head[0] == -2 && head[1] == -1 )    
            code = "Unicode";    
        else if(head[0]==-17 && head[1]==-69 && head[2] ==-65)    
            code = "UTF-8";    
            
        inputStream.close();  
          
        System.out.println(code);   
        return code;  
    }  
      
}
登入後複製

注意:在resolveTxt方法中不能透過readTxt方法傳InputStream流,這樣會使兩個方法持有同一個流引用,而在resolveTxt方法中已讀過流中的三個字節,流中的pos此時已經是3了,而不是流的起始位置,再在readTxt中讀取時就會出現IOException:Read Error。

更多java知識請關注java基礎教學欄。

以上是java讀取txt檔案亂碼解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!