Java缓冲字节流BufferedOutputStream(输出),BufferedInputStream(输入),对文件的复制操作


	public static void main(String[] args) throws IOException {
		//bufferedOutput();
		//bufferedInput();
		//copyFile();
	}
	/**
	 * 文件复制操作(操作文件最快)
	 * @throws IOException
	 */
	private static void copyFile() throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test.avi"));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\test.avi"));
		byte[] bytes = new byte[1024];
		int len = 0;
		while((len = bis.read(bytes)) != -1){
			bos.write(bytes, 0, len);
		}
		bos.close();
		bis.close();
		System.out.println("复制完成");
		
	}
	/**
	 * BufferedInputStream:提升读取效率,执行顺序和BufferedOutputStream一样
	 */
	private static void bufferedInput() throws IOException{
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
		byte[] b = new byte[1024];
		int len = 0;
		while((len = bis.read(b)) != -1){
			System.out.println(new String(b, 0, len));
		}
		bis.close();//关闭缓冲流也会吧字节流关闭
	}
	/**
	 * BufferedOutputStream:提升写入效率
	 * 	没提升效率前顺序:程序->JVM->计算机底层写入(每次读取*字节,每次都调用一次计算机底层)
	 * 提升效率后:程序->JVM(先不调用计算机底层,先将数据缓冲,数据读取完毕后->计算机底层)
	 * OutputStream的子类,使用方法都一样
	 */
	private static void bufferedOutput() throws IOException{
		FileOutputStream fos = new FileOutputStream("D:\\a.txt");
		BufferedOutputStream bos = new BufferedOutputStream(fos);//传入要提升效率的字节输出流
		bos.write("HelloWorld".getBytes());
		bos.close();
		System.out.println("写入完成");
	}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值