设为首页 加入收藏

TOP

Java实现本地 fileCopy
2015-08-31 21:25:28 来源: 作者: 【 】 浏览:483
Tags:Java 实现 本地 fileCopy

前言:


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可查看!)
以上内容纯属个人学习总结,不代表任何团体或单位。若有理解不到之处请见谅!


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java反序列化测试 下一篇Struts2拦截器模拟

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: