io拷贝文件

package com.cebbank.doa.test;
import java.io.*;
public class IOCopy {
	public static void main(String[] args){
		   IOCopy copy = new IOCopy();
//		   copy.copyByByte1();
		   copy.copyByByte2();  //推荐这个 速度快
//		   copy.copyByByte3();
	}
	
	public void copyByByte1(){
	   try{
	    FileInputStream fis = new FileInputStream("d:/11.xls");
	    FileOutputStream fos = new FileOutputStream("d:/22.xls");
	   
	    int b = fis.read();
	    while(b != -1){
	     fos.write(b);
	     b = fis.read();
	    }
	    fis.close();
	   }catch(IOException e){
	    e.printStackTrace();
	   }
	}

	public void copyByByte2(){
	   try{
	    FileInputStream fis = new FileInputStream("d:/11.xls");
	    FileOutputStream fos = new FileOutputStream("d:/22.xls");
	   
	    byte[] b = new byte[1024];
	    int n = fis.read(b,0,24);
	    while(n != -1){
	     fos.write(b,0,n);
	     n = fis.read(b,0,24);
	    }
	    fis.close();
	   }catch(IOException e){
	    e.printStackTrace();
	   }
	}

	public void copyByByte3(){
	   try{
	    FileInputStream fis = new FileInputStream("d:/11.xls");
	    FileOutputStream fos = new FileOutputStream("d:/22.xls");
	   
	    byte[] b = new byte[1024];
	    int n = fis.read(b,100,20);
	    while(n != -1){
	     fos.write(b,100,n);
	     n = fis.read(b,100,20);
	    }
	    fis.close();
	   }catch(IOException e){
	    e.printStackTrace();
	   }
	}

	public void copyByCharactor(){
	   try{
	    BufferedReader br = new BufferedReader(new FileReader("d:/11.xls"));
	    BufferedWriter bw = new BufferedWriter(new FileWriter("d:/22.xls"));
	   
	    String line = br.readLine();
	    int j = 0;
	    while(line != null){
	     bw.write(line);
	    
	     line = br.readLine();
	     if(line != null)
	      bw.newLine();
	    }
	    bw.close();
	    br.close();
	   }catch(IOException e){
	    e.printStackTrace();
	   }
	}

	public void copyByCharactor1(){
	   try{
	    BufferedReader br = new BufferedReader(new FileReader("d:/11.xls"));
	    PrintWriter pw = new PrintWriter(new FileWriter("d:/22.xls"));
	   
	    String line = br.readLine();

	    while(line != null){
	     pw.print(line);
	     line = br.readLine();
	     if(line!=null){
	      pw.println();
	     } 
	    }
	    pw.close();
	    br.close();
	   }catch(IOException e){
	    e.printStackTrace();
	   }
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值