2016-07-24
Reader
1,InputStreamReader 字节流通向字符流的桥梁
FileInputStream fis=null;
InputStreamReader isr=null;
fis=new FileInputStream( "C:\\Users\\admin\\Desktop\\in.txt");
isr=new InputStreamReader(fis,"UTF-8");
package com.java1995; public class Test { public static void main(String[] args) { System.out.println(System.getProperty("file.encoding")); System.out.println("大家好".getBytes().length); } }
package com.java1995; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public abstract class TestInputStreamReader { public static void main(String[] args) { FileInputStream fis = null; InputStreamReader isr = null; try { fis = new FileInputStream("D:\\workspace\\java_io\\in.txt"); isr = new InputStreamReader(fis, "UTF-8"); int c = 0; fis.read();// 每次读取一个字节 c = isr.read();// 每次读取的字节是根据编码集决定的 while (c != -1) { System.out.print((char) c); c = isr.read(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // 关闭流 // 先关闭fis,然后关闭isr } } }
【参考资料】
时间: 2024-10-06 12:04:02