首頁  >  文章  >  Java  >  java讀取txt中文亂碼

java讀取txt中文亂碼

尚
原創
2019-11-21 12:01:283626瀏覽

java讀取txt中文亂碼

java讀取txt文字中如含有中文,可能會出現亂碼,解決方案是: 

1、要統一編碼,java工程的編碼,txt文本編碼,java工程中的java文字編碼都統一為utf-8; 

2、利用InputStreamReader(new FileInputStream(fileUrl), "utf-8")將文字再次設定為utf-8 

InputStreamReader isr;   
    try {   
        isr = new InputStreamReader(new FileInputStream(fileUrl), "utf-8");   
         BufferedReader read = new BufferedReader(isr);   
           String s=null;   
           List<String> list = new ArrayList<String>();   
           while((s=read.readLine())!=null)   
           {   
               //System.out.println(s);   
               if(s.trim().length()>1){   
               list.add(s.trim());   
               }   
           }   
              
           System.out.println("OK!");   
    } catch (UnsupportedEncodingException e) {   
               e.printStackTrace();   
    } catch (FileNotFoundException e) {   
               e.printStackTrace();   
    } catch (IOException e) {   
               e.printStackTrace();   
    }

更多java知識請關注java基礎教程

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn