字节流复制

package copy;

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

import javax.management.RuntimeErrorException;

public class CopyByte {
public static void main(String[] args) {
long start=System.currentTimeMillis();
//FileStreamByteCopy(); //共消耗382毫秒
//FileStreamCopy(); //共消耗265929毫秒
BufferedStreamByteCopy(); //共消耗210毫秒
//BufferedStreamCopy(); //共消耗1348毫秒
long end=System.currentTimeMillis();
System.out.println("复制成功,共消耗"+(end-start)+"毫秒");
}
//缓存区字节数组复制
//共消耗210毫秒
public static void BufferedStreamByteCopy() {
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis=new BufferedInputStream(new FileInputStream("d:\\2.zip"));
bos=new BufferedOutputStream(new FileOutputStream("h:\\2.zip"));
byte[] bytes=new byte[1024];
int len=0;
while ((len=bis.read(bytes))!=-1) {
bos.write(bytes,0,len);
}

} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("复制失败");
}finally{
try {
if (bos!=null) {
bos.close();
}
} catch (Exception e2) {
// TODO: handle exception
throw new RuntimeException("资源释放失败");
}finally{
try {
if (bis!=null) {
bis.close();
}
} catch (Exception e3) {
// TODO: handle exception
throw new RuntimeException("资源释放失败");
}
}
}
}
//缓存区字节复制
//共消耗1348毫秒
public static void BufferedStreamCopy() {
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis=new BufferedInputStream(new FileInputStream("d:\\1.avi"));
bos=new BufferedOutputStream(new FileOutputStream("e:\\4.avi"));
int len=0;
while ((len=bis.read())!=-1) {
bos.write(len);
}

} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("复制失败");
}finally{
try {
if (bos!=null) {
bos.close();
}
} catch (Exception e2) {
// TODO: handle exception
throw new RuntimeException("资源释放失败");
}finally{
try {
if (bis!=null) {
bis.close();
}
} catch (Exception e3) {
// TODO: handle exception
throw new RuntimeException("资源释放失败");
}
}
}
}
//字节流数组复制
//共消耗382毫秒
public static void FileStreamByteCopy() {
FileInputStream fis = null;
FileOutputStream fos = null;

try{
fis = new FileInputStream("d:\\1.avi");
fos = new FileOutputStream("e:\\1.avi");
//定义字节数组,长度1024整数倍
byte[] bytes = new byte[1024];
//定义变量,保存read方法返回值
int len = 0 ;
//开始循环读取,读取一个字节数组,写一个字节数,0,有效个数
while((len = fis.read(bytes))!=-1){
fos.write(bytes, 0, len);
}

}catch(IOException ex){
throw new RuntimeException("文件复制失败");
}finally{
try{
if(fos!=null)
fos.close();
}catch(IOException ex){
throw new RuntimeException("关闭资源失败");
}finally{
try{
if(fis!=null)
fis.close();
}catch(IOException ex){
throw new RuntimeException("关闭资源失败");
}
}
}
}
//字节流复制
//共消耗265929毫秒
public static void FileStreamCopy() {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream("D:\\1.avi");
fos=new FileOutputStream("e:\\2.avi");
int len=0;
while ((len=fis.read())!=-1) {
fos.write(len);
}
} catch (Exception e) {
// TODO: handle exception
throw new RuntimeException("复制失败");
}finally{
try {
if (fos!=null) {
fos.close();
}
} catch (Exception e2) {
// TODO: handle exception
throw new RuntimeException("资源释放失败");
}finally{
try {
if (fis!=null) {
fis.close();
}
} catch (Exception e3) {
// TODO: handle exception
throw new RuntimeException("资源释放失败");
}
}

}
}
}

时间: 2024-08-09 18:18:44

字节流复制的相关文章

字节流复制mp3文件(带缓冲区)

//自定义的缓冲区 import java.io.*; class  MyBufferedInputStream{    private byte[] buf = new byte[1024]; private InputStream in;        private int pos = 0, count = 0; MyBufferedInputStream(InputStream in){        this.in = in;    }        /*        一次读一个字节

字节流复制一个图片

1.用字节流读取对象和图片关联 2.用字节流写入流对象,创建一个图片文件. 3.通过循环读写,完成数据存储. 4.关闭资源 实例代码: import java.io.*; class CopyPic { public static void main(String[] args) { FileOutputStream fos = null; FileInputStream fis = null; try { fos = new FileOutputStream("c:\\2.jpg")

使用字节流复制一个文件夹

package com.tanlei.Demo; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /* * 使用字节流复制一个文件夹 */ public class CopyDirDemo { public static void main(S

IO流,字节流复制文件,字符流+缓冲复制文件

JAVAIO如果按流向分:输入流和输出流两种 输入流的基类:InputStream   Reader 输出流的基类:OutputStream   Writer 如果按数据单元划分:字节流和字符流 字节流输入输出的基类:InputStream  OutputStream 字符流输入输出的基类:Reader   Writer 字节流复制文件内容 public static void main(String[] args) { //字节流复制文件内容 InputStream io=null; Outp

简单利用缓冲字节流复制图片

/* ****使用缓冲字节流的好处就是可以加快读取效率.执行效率. 1.找到一个目标文件.(想要复制的目标图片) 目的路径(复制到目的路径) 2.建立通道 FileInputStream 建立通道 FileOutputStream 3.创建一个缓冲字节输入流 里面传入一个InputStream. 创建一个缓冲字节输出流 里面传入一个OutputStream. 4.读取数据 5.关闭资源(先开后关,后开先关的原则) */ import java.io.BufferedInputStream;imp

字节流复制视频的四种方式

package com.io.liushuaishuai; /* 四种方式复制视频 记录时间 1 基本字节流一次一个字节 2基本字节流一次一个字节数组 3字节缓冲流1 基本字节流一次一个字节 4字节缓冲流一次一个字节数组 */ import java.io.*; public class CopyMp4Demo { public static void main(String[] args) throws IOException { //记录开始时间 long start = System.cu

字节流复制文件

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.LinkedList;import java.util.List; //文件复制 //E:/3.jpg ---> D:/1.jpgpublic class CopyFileByIo { public static void main(String[] args) { FileInput

用IO字节流复制文件-CopyFileByIo

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.LinkedList; import java.util.List; //文件复制 //E:/3.jpg ---> D:/3copy.jpg public class CopyFileByIo { public static void main(String[] args) {

字节流复制文件和字节缓冲流复制文件的时间比较

package cn.lijun.demo2; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; public class Copy { public st