【JavaSE学习笔记】IO流05_字符缓冲输出流BufferedWriter、字符缓冲输入流BufferedReader

IO流05

A.字符缓冲输出流

1)概述

将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

2)构造方法

public BufferedWriter(Writer out):创建字符缓冲输出流的对象

默认的缓冲区就足够大了,所以不用指定缓冲区大小!

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Demo01 {
	public static void main(String[] args) throws Exception {
		// 创建字符缓冲输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
		
		// 写数据
		bw.write("hello");
		bw.write("world");
		bw.write("java");
		
		// 关闭资源
		bw.close();
	}
}

3)特殊功能

public void newLine() throw IOException:写入一个行的分隔符号(换行)

import java.io.BufferedWriter;
import java.io.FileWriter;

public class Demo02 {
	public static void main(String[] args) throws Exception {
		// 创建字符缓冲输出流对象
		BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));

		// 写数据
		for (int i = 0; i < 10; i++) {
			bw.write("hello" + i);

			// 换行
			bw.newLine();

			bw.flush();
		}

		bw.close();
	}
}

B.字符缓冲输入流

1)构造方法

public BufferedReader(Reader in):创建一个使用默认大小输入缓冲区的缓冲字符输入流

import java.io.BufferedReader;
import java.io.FileReader;

public class Demo03 {
	public static void main(String[] args) throws Exception {
		// 创建字符缓冲输入流对象
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));

		// 一个字符一个字符读取
		// int ch = 0 ;
		// while((ch=br.read())!=-1){
		// System.out.print((char)ch);
		// }

		// 一次读取一个字符数组
		char[] chs = new char[1024];
		int len = 0;
		while ((len = br.read(chs)) != -1) {
			System.out.println(new String(chs, 0, len));
		}
		
		br.close();
	}
}

2)特殊功能

public String readLine() throws IOException():一次读取一行

包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 

import java.io.BufferedReader;
import java.io.FileReader;

public class Demo04 {
	public static void main(String[] args) throws Exception {
		// 创建字符缓冲输入流对象
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));

		String line = null;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
		
		br.close();
	}
}


C.应用

1)将当前项目下的a.txt------>b.txt文件中(按字符数组操作)

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

public class Demo05 {
	public static void main(String[] args) {
		
		BufferedReader br = null;
		BufferedWriter bw = null;
		
		//封装对象
		try {
			br = new BufferedReader(new FileReader("a.txt"));
			bw = new BufferedWriter(new FileWriter("b.txt"));
			
			char[] chs = new char[1024];
			int len = 0;
			while ((len = br.read(chs)) != -1) {
				bw.write(chs, 0, len);
				bw.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bw.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

2)将当前项目下的a.txt------>b.txt文件中(按行操作)

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

public class Demo06 {
	public static void main(String[] args) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		
		//封装对象
		try {
			br = new BufferedReader(new FileReader("a.txt"));
			bw = new BufferedWriter(new FileWriter("b.txt"));
			
			String line = null;
			while ((line = br.readLine()) != null) {
				bw.write(line);
				bw.newLine();
				bw.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				bw.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


你可能感兴趣的:(JavaSE,io流,字符缓冲输出流,字符缓冲输入流,BufferedWriter,BufferedReader)