Java实现本地 fileCopy
2015-07-31 20:23
567 查看
前言:
Java中流是重要的内容,基础的文件读写与拷贝知识点是很多面试的考点。故通过本文进行简单测试总结。
(附:参考代码时注意准备好测试文件,不然出现异常“file can't be found”!
文件拷贝到Java工程的直接目录下,刷新project可查看!
)
Java中流是重要的内容,基础的文件读写与拷贝知识点是很多面试的考点。故通过本文进行简单测试总结。
2.图展示【文本IO/二进制IO】(这是参考自网上的一张总结图,相当经典,方便对比记忆)
3.文本复制的实验Java实现code:
package com.gdufe.io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class TestInputOutputStream { // private final static String SOURCE="t.txt"; // private final static String TARGET="tt.txt"; // private final static String SOURCE="p.png"; // private final static String TARGET="pp.png"; private final static String SOURCE="D:\\Tool_Software\\mysql-installer-community-5.6.23.0.msi"; private final static String TARGET="mysql-installer-community-5.6.23.0.msi"; //D:\Tool_Software\Ryuyan.zip public static void main(String[] args) { // TestInputOutputStream.copy1(SOURCE, TARGET); // TestInputOutputStream.copy2(SOURCE, TARGET); TestInputOutputStream.copy3(SOURCE, TARGET); System.out.println("--End---"); } public static void copy1(String src,String tar){ InputStream input = null; OutputStream output = null; try{ input = new FileInputStream(src); output = new FileOutputStream(tar); int r; while((r=input.read())!=-1){ output.write((byte)r); } }catch(Exception e){ e.printStackTrace(); }finally{ try { input.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void copy2(String src,String tar){ InputStream input = null; OutputStream output = null; try{ input = new FileInputStream(src); output = new FileOutputStream(tar); int length=0; byte []b = new byte[1024]; while((length=input.read(b))!=-1){ output.write(b,0,length); } }catch(Exception e){ e.printStackTrace(); }finally{ try { input.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void copy3(String src,String tar){ InputStream input = null; OutputStream output = null; try{ input = new DataInputStream(new BufferedInputStream(new FileInputStream(src))); output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(tar))); /***经过亲测,buffer缓冲流读取时确实更快一些****/ int length=0; byte []b = new byte[1024]; //先将stream读入字节数组 while((length=input.read(b))!=-1){ output.write(b,0,length); } }catch(Exception e){ e.printStackTrace(); }finally{ try { input.close(); output.close(); } catch (IOException e) { e.printStackTrace(); } } } }
(附:参考代码时注意准备好测试文件,不然出现异常“file can't be found”!
文件拷贝到Java工程的直接目录下,刷新project可查看!
)
相关文章推荐
- spring整合mongo报错:Timed Out While Waiting For A Server That Matches AnyServerSelector{} After 10000 Ms
- java类的特性
- Java面向对象的三大特征
- Eclipse中导入项目Js文件报错
- spring 声明式事务管理注解方式实现
- Java虚拟机七种垃圾收集器比较
- 【转自看雪】反编译apk+eclipse中动态调试smali
- spring 声明式事务管理xml方式实现
- Java 修饰符
- JAVA EE 自学路线
- JUnit4---eclipse实现java代码单元测试
- java多线程基础部分探究
- java.多线程
- java 当中的多态,面向对象,方法的重载,Final关键字
- Struts框架之文件的上传
- Java学习第4天>>面向对象1
- Error/Exception-01-hibernate(jar包冲突)--javax.persistence.OneToMany.orphanRemoval()Z异常
- java中注解的使用与实例 (二)
- Struts2中Theme与Template
- Java 序列化