Java中文件的拷贝
2016-07-17 22:46
267 查看
一、字节流的基本知识:
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)
二、我们分别用单字节的方法,批量字节的方法,以及带缓冲区的方法来拷贝文件,比较一下各自的性能
源代码:
运行结果:
通过运行结果可知:批量字节处理程序所花时间最少!
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(); } } }
运行结果:
通过运行结果可知:批量字节处理程序所花时间最少!
相关文章推荐
- java内部类种类解读
- JAVA继承和多态
- 有一百盏灯,全部亮着并且从一到一百进行编号,对每盏灯做如下处理,如果该灯的编号能被1整除则拨一下开关,能被二整除再拨一下,以此类推,直到该灯的编号(不含编号本身)为至,问最后哪几盏灯亮着。
- eclipse 向HDFS中创建文件夹报错 permission denied
- java中的spring框架(1)
- maven打包错误: -source 1.5 中不支持 diamond 运算符以及@Override is not allowed when implementing interface method
- [原创]java WEB学习笔记48:其他的Servlet 监听器:域对象中属性的变更的事件监听器 (3 个),感知 Session 绑定的事件监听器(2个)
- JAVA和.NET互调用
- hdu1250(Java)大数相加的问题
- java基础知识
- 遍历map的方法
- Java类库及其组织结构
- 数组中重复的数字 java
- RxJava 与 Retrofit 结合的最佳实践
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- JavaWeb(四)
- JavaWeb(三)
- Java线程详解
- javaee之Servlet的理解
- javaweb之请求转发和请求包含