2016-07-24
字符流相关类:读取缓冲类
1,BufferedReader
package com.java1995; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; public class TestBufferedReader { public static void main(String[] args) { InputStreamReader isr = null; BufferedReader br = null; try { isr = new InputStreamReader(new FileInputStream("D:\workspace\java_io\in.txt"), "UTF-8"); br = new BufferedReader(isr, 1000); br.mark(100); // 第一种,使用read() int c = 0; c = br.read(); while (c != -1) { System.out.print((char) c); c = br.read(); } // 第二种,使用readLine() System.out.println("------------------"); br.reset(); String str = br.readLine(); while (str != null) { System.out.println(str); str = br.readLine(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { isr.close(); br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
【参考资料】