IO:字节流转化字符流
InputStreamReader类可以将InputStream字节流转化成Reader字符流:
1 void inputStreamReader() { 2 // InputStreamReader类可以将InputStream字节流转化成Reader字符流 3 // 中文无乱码 4 File file = new File("e:\\test.txt"); 5 InputStream inputStream = null; 6 InputStreamReader isr = null; 7 char[] ch = new char[24]; 8 int len; 9 StringBuilder sb = new StringBuilder(); 10 try { 11 inputStream = new FileInputStream(file); 12 isr = new InputStreamReader(inputStream); 13 while ((len = isr.read(ch)) != -1) { 14 sb.append(ch); 15 } 16 System.out.println(sb); 17 // 获取文件编码格式 18 System.out.println(isr.getEncoding()); 19 } catch (FileNotFoundException e) { 20 // TODO Auto-generated catch block 21 e.printStackTrace(); 22 } catch (IOException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } finally { 26 if (isr != null) { 27 try { 28 isr.close(); 29 } catch (IOException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 } 35 }