缓冲流---为字节流和字符流复制文件增加缓冲流

缓冲流——增强性能

字节流的缓冲流(均未增加新方法)

BufferedInputStream

BufferedOutputStream

字节符的缓冲流

方法名称方法作用
readLine()返回值为String对象,读取一行
newLine()换行符

字节流的缓冲流代码

package cn.hxh.io.buffered;

import java.io.*;

public class BufferedByte {

	public static void main(String[] args) throws IOException {
		CopyFile("D:/aa/a.txt", "D:/aa/b.txt");
	}

	public static void CopyFile(String srcPath, String destPath) throws IOException {
		CopyFile(new File(srcPath), new File(destPath));
	}

	public static void CopyFile(File src, File dest) throws IOException {
		if (src.isDirectory()) {
			throw new IOException("这是一个文件夹");
		}
		InputStream iStream = new BufferedInputStream(new FileInputStream(src));
		OutputStream oStream = new BufferedOutputStream(new FileOutputStream(dest));
		byte[] flush = new byte[1024];
		int len = 0;
		while (-1 != (len = iStream.read(flush))) {
			oStream.write(flush, 0, len);
		}
		oStream.flush();
		oStream.close();
		iStream.close();
	}

}

字符流的缓冲流代码

package cn.hxh.io.buffered;

import java.io.*;

public class BufferedChar {
	public static void main(String[] args) {
		File in = new File("D:/aa/a.txt");
		File out = new File("D:/aa/b.txt");
		BufferedReader re = null;
		BufferedWriter wr = null;
		String line = null;
		try {
			re = new BufferedReader(new FileReader(in));
			wr = new BufferedWriter(new FileWriter(out));
			while (null != (line = re.readLine())) {
				wr.write(line);
				wr.newLine();
			}
			wr.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				if (wr != null)
					wr.close();
				if (re != null)
					re.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值