前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >缓冲流---为字节流和字符流复制文件增加缓冲流

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

作者头像
shimeath
发布2020-07-30 17:09:31
4070
发布2020-07-30 17:09:31
举报

缓冲流——增强性能

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

BufferedInputStream

BufferedOutputStream

字节符的缓冲流

方法名称

方法作用

readLine()

返回值为String对象,读取一行

newLine()

换行符

字节流的缓冲流代码

代码语言:javascript
复制
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();
	}

}

字符流的缓冲流代码

代码语言:javascript
复制
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();
			}

		}
	}
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-01-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字节流的缓冲流(均未增加新方法)
    • BufferedInputStream
      • BufferedOutputStream
      • 字节符的缓冲流
        • 字节流的缓冲流代码
          • 字符流的缓冲流代码
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档