javaIO流之字节流的四种方式复制文件方式总结

/*

* 需求:把e:\\a.mp4复制到当前项目目录下的copy.mp4中

*

* 字节流四种方式复制文件:

* 基本字节流一次读写一个字节: 共耗时:117235毫秒

* 基本字节流一次读写一个字节数组: 共耗时:156毫秒

* 高效字节流一次读写一个字节: 共耗时:1141毫秒

* 高效字节流一次读写一个字节数组: 共耗时:47毫秒

*/

package cn.itcast_06;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyMp4Demo {
	public static void main(String[] args) throws IOException {
		long start = System.currentTimeMillis();
		// method1("e:\\a.mp4", "copy1.mp4");
		// method2("e:\\a.mp4", "copy2.mp4");
		// method3("e:\\a.mp4", "copy3.mp4");
		method4("e:\\a.mp4", "copy4.mp4");
		long end = System.currentTimeMillis();
		System.out.println("共耗时:" + (end - start) + "毫秒");
	}

	// 高效字节流一次读写一个字节数组:
	public static void method4(String srcString, String destString)
			throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcString));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destString));

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = bis.read(bys)) != -1) {
			bos.write(bys, 0, len);
		}

		bos.close();
		bis.close();
	}

	// 高效字节流一次读写一个字节:
	public static void method3(String srcString, String destString)
			throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				srcString));
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream(destString));

		int by = 0;
		while ((by = bis.read()) != -1) {
			bos.write(by);

		}

		bos.close();
		bis.close();
	}

	// 基本字节流一次读写一个字节数组
	public static void method2(String srcString, String destString)
			throws IOException {
		FileInputStream fis = new FileInputStream(srcString);
		FileOutputStream fos = new FileOutputStream(destString);

		byte[] bys = new byte[1024];
		int len = 0;
		while ((len = fis.read(bys)) != -1) {
			fos.write(bys, 0, len);
		}

		fos.close();
		fis.close();
	}

	// 基本字节流一次读写一个字节
	public static void method1(String srcString, String destString)
			throws IOException {
		FileInputStream fis = new FileInputStream(srcString);
		FileOutputStream fos = new FileOutputStream(destString);

		int by = 0;
		while ((by = fis.read()) != -1) {
			fos.write(by);
		}

		fos.close();
		fis.close();
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 06:34:31

javaIO流之字节流的四种方式复制文件方式总结的相关文章

转 四种操作xml的方式(SAX, DOM, JDOM, DOM4J)比较

四种操作xml的方式(SAX, DOM, JDOM, DOM4J)比较 (2010-10-29 15:31:28) 转载▼  http://blog.sina.com.cn/s/blog_6458bf440100mgjs.html 标签: 杂谈 分类: JAVA技术 1 介绍 引子:XML自从出现以来,以其可扩展性.自描述性.自相容性等优点,被誉为信息标准化过程的有力工具,基于XML的标准将成为以后信息标准的主流.伴随而生的是针对XML的操作技术,“名人总的有人去解析”吗!下面是愚人对目前主要的

四种保存数据的方式

转载地址:http://blog.csdn.net/tianyitianyi1/article/details/7713103 在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyedArchiver:采用归档的形式来保存数据,该数据对象需要遵守NSCoding协议,并且该对象对应的类必须提供encodeWithCoder:和initWithCoder:

java中的四种单例实现方式

在java中,单例设计模式是非常常见的设计模式,对单例设计模式的概念,不做过多的介绍,下面将逐一介绍四种单例设计模式: 1.第一种单例设计模式 1.1 代码实现 package com.singleton.one; /**  * 第一种单例设计模式  * @author Administrator  *  */ public class SingleTonOne { // 实例化 private static SingleTonOne instance = new SingleTonOne();

oracle四种访问数据库的方式

1.sql plus——命令操作: 数据库自带的命令工具,通过此工具可以在dos窗口中直接对数据库进行操作.使用此工具有两种进入方式: (1).在安装后的数据库下有一个应用程序开发,子目录里有一个sql plus,直接单击打开,就可以输入用户名.密码登录数据库进行操作. (2).直接打开windows的dos窗口(win键+R—>cmd命令回车进入),输入sql plus命令然后输入用户名密码即可以进入. 2.sql developer——图形化界面操作: 数据库自带的图形化操作工具,通过该工具

tp-02 四种url访问的方式

1:http://localhost/index.php?m=模块&c=控制器&a=操作方法 [get模式] 2:http://localhost/index.php/模块[模块文件夹]/控制器/操作方法 [pathinfo模式] 3:http://localhost/模块[模块文件夹]/控制器/操作方法 [rewite重写模式] 4:http://localhost/index.php?s=/模块[模块文件夹]/控制器/操作方法 [兼容模式] 具体的url模式 在ThinkPHP/con

Selenium系列(十一) - 针对两种上传文件方式的实现方案

如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识,需要自己去补充哦,博主暂时没有总结(虽然我也会,所以我学selenium就不用复习前端了哈哈哈...) 首先,将下面html代码保存到一个文件中 后续第一种上传文件方式的代码小案例都是访问此html的 <!DOCTYPE html> <html lang="en">

Tomcat的四种web应用部署方式详解

在Tomcat中有四种部署Web应用的方式,简要的概括分别是: (1)利用Tomcat自动部署 (2)利用控制台进行部署 (3)增加自定义的Web部署文件(%Tomcat_Home%\conf\Catalina\localhost\AppName.xml) (4)手动修改%Tomcat_Home%\conf\server.xml文件来部署web应用 第一种方式:利用Tomcat自动部署 利用Tomcat自动部署方式是最简单的.最常用的方式.若一个web应用结构为D:\workspace\WebA

SpringBoot:四种读取properties文件的方式

前言 在项目开发中经常会用到配置文件,配置文件的存在解决了很大一份重复的工作.今天就分享四种在Springboot中获取配置文件的方式. 注:前三种测试配置文件为springboot默认的application.properties文件 [html] view plain copy #######################方式一######################### com.battle.type3=Springboot - @ConfigurationProperties c

post请求四种传送正文的方式

一.简介 HTTP协议规定post提交的数据必须放在消息主体(entity-body)中,但协议没有规定数据必须使用什么编码方式.HTTP协议是以ASCII码传输,建立再TCP/IP协议之上的应用层规范.HTTP请求分为3个部分:状态行.请求头和消息主体.类似于: <method> <request-URL> <version> <headers> <entity-body> 服务端通常是根据请求头(header)中的Content_Type字段