java 讀取檔案亂碼

來源:互聯網
上載者:User

標籤:int   fileinput   private   gbk   nbsp   amd   set   log   alt   

 private void filereader() throws IOException{
  BufferedReader bf= new BufferedReader(new InputStreamReader(new FileInputStream("D:\\CompareDll.py"),"UTF-8"));
  String str="";
  while((str=bf.readLine())!=null){
   System.out.println(str);
   
  }
  bf.close();
 }

如果不指定編碼則讀取檔案時使用系統預設編碼,一般為gbk

Java的I/O類處理

Reader 類是 Java 的 I/O 中讀字元的父類,而 InputStream 類是讀位元組的父類,InputStreamReader 類就是關聯位元組到字元的橋樑,它負責在 I/O 過程中處理讀取位元組到字元的轉換,而具體位元組到字元的解碼實現它由 StreamDecoder 去實現,在 StreamDecoder 解碼過程中必須由使用者指定 Charset 編碼格式。值得注意的是如果你沒有指定 Charset,將使用本地環境中的預設字元集,例如在中文環境中將使用 GBK 編碼。

java 讀取檔案亂碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.