一、字节流的基本知识:
1.输入流:InputStream (in ) 输出流:OutputStream (out)
2.输入流的基本方法:
*int b = in.read(); //读取一个字节
*int len =in.read(byte [ ] buf); //读取一个字节数组
*int len = in.read(byte [ ] buf,int start, int size) //指定从字节组的哪里开始读
3.输出流的基本方法:(与输入流相对应)
*out.write();
*out.write(byte [ ] buf);
*out.write(byte [ ] buf,int start, int size)
二、我们分别用单字节的方法,批量字节的方法,以及带缓冲区的方法来拷贝文件,比较一下各自的性能
源代码:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileCopy {
//单字节拷贝方法
public void copyByByte(File srcFile,File desFile) throws IOException{
try {
if(!srcFile.exists()){ //如果文件不存在,抛出文件不存在的异常
throw new IllegalArgumentException(srcFile+"不存在");
}
if(!srcFile.isFile()) //如何给定的路径不是文件,抛出不是文件的异常
throw new IllegalArgumentException(srcFile+"不是文件");
InputStream is = new FileInputStream(srcFile);
OutputStream os = new FileOutputStream(desFile);
int b=0;
while((b=is.read())!=-1){ //直到读到文件末尾时结束读
os.write(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//批量字节拷贝
public void copyByBufByte(File srcFile,File desFile) throws IOException{
try {
if(!srcFile.exists()){
throw new IllegalArgumentException(srcFile+"不存在");
}
if(!srcFile.isFile())
throw new IllegalArgumentException(srcFile+"不是文件");
InputStream is = new FileInputStream(srcFile);
OutputStream os = new FileOutputStream(desFile);
byte [] buf = new byte[1024*4];
int len =0;
while((len=is.read(buf, 0, buf.length))!=-1){
os.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void copyByBuffered(File srcFile,File desFile) throws IOException{
if(!srcFile.exists()){
throw new IllegalArgumentException(srcFile+"不存在");
}
if(!srcFile.isFile())
throw new IllegalArgumentException(srcFile+"不是文件");
FileInputStream is = new FileInputStream(srcFile);
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream os = new FileOutputStream(desFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int len = bis.read();
while(len!=-1){
bos.write(len);
len = bis.read();
}
}
public static void main(String[] args) {
FileCopy copy = new FileCopy();
File srcFile =new File("e:\\演员.mp3");
File desFile = new File("e:\\1演员.mp3");
File desFile1 = new File("e:\\2演员.mp3");
File desFile2 = new File("e:\\3演员.mp3");
try {
//分别计算一下各自的拷贝时间
long start1 = System.currentTimeMillis();
copy.copyByByte(srcFile, desFile);
long end1 =System.currentTimeMillis();
System.out.print("单字节拷贝所花的时间:");
System.out.println(end1-start1);
long start2 = System.currentTimeMillis();
copy.copyByBufByte(srcFile, desFile1);
long end2 =System.currentTimeMillis();
System.out.print("批量字节拷贝所花的时间:");
System.out.println(end2-start2);
long start3 = System.currentTimeMillis();
copy.copyByBuffered(srcFile, desFile2);
long end3 =System.currentTimeMillis();
System.out.print("带缓冲字节拷贝所花的时间:");
System.out.println(end3-start3);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
通过运行结果可知:批量字节处理程序所花时间最少!