Java io流常用流类 BufferedReader读入详解

        BufferedReader类用于缓冲读取字符,将字节流封装成BufferedReader对象,然后用readLine()方法逐行读取,然后返回,我们通过循环使用StringBuffer的append()方法接受,或者通过循环直接在控制台上输出。


此为通过循环直接在控制台输出

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Buffered3 {
	
	public static void main(String[] args){
		
		BufferedReader  br = null;
		FileReader      fr = null;

		try {
			fr  = new FileReader("E:\\haha\\jie.doc");
			br  = new BufferedReader(fr);
			String next = "";
			while((next=br.readLine())!=null){
				System.out.println(next);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}

此为通过StringBuffer接受然后用append()方法组合输出:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Buffered3 {
	
	public static void main(String[] args){
		
		BufferedReader  br = null;
		FileReader      fr = null;

		try {
			fr  = new FileReader("E:\\haha\\jie.doc");
			br  = new BufferedReader(fr);
			String next = "";
			StringBuffer sbff = new StringBuffer();
			while((next=br.readLine())!=null){
				sbff.append(next);
			}
			System.out.println(sbff.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}


你可能感兴趣的:(Java io流常用流类 BufferedReader读入详解)