常用文件复制方法

1、文件字节流

package com.java.copyFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyByInputStreamAndOutputStream {
	public static void main(String[] args) {
		long time1=System.currentTimeMillis();
//		File source=new File("E:\\Java编程思想 第四版习题答案.pdf");
//		File target=new File("F:\\Java编程思想 第四版习题答案.a");
		File source=new File("F:\\迅雷下载\\[电影天堂-www.dy2018.net]霸王别姬BD国语中字.rmvb");
		File target=new File("E:\\Java编程思想 第四版习题答案.a");
		try {
			Copy( source, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time2=System.currentTimeMillis();
		System.out.println(time2-time1);
	}
	
	
	public static void Copy(File source,File target) throws IOException{		
			if(target.exists())
				target.delete();
			FileInputStream  fis=new FileInputStream(source);
			FileOutputStream fos=new FileOutputStream(target);
			byte bys[]=new byte[1024];
			int len=0;
			while((len=fis.read(bys))!=-1)
				fos.write(bys);
			fis.close();
			fos.close();
	}
		
}	
	
	

2、字节缓冲流

package com.java.copyFile;

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;

public class CopyByBufferInputStreamAndBufferOutPutStream {

	public static void main(String[] args) {
		long time1=System.currentTimeMillis();
		File source=new File("F:\\迅雷下载\\[电影天堂-www.dy2018.net]霸王别姬BD国语中字.rmvb");
		File target=new File("E:\\Java编程思想 第四版习题答案.a");
		try {
			Copy( source, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time2=System.currentTimeMillis();
		System.out.println(time2-time1);

	}

	private static void Copy(File source, File target) throws IOException {
		if(target.exists())
			target.delete();
			FileInputStream fis=new FileInputStream(source);
			BufferedInputStream bis=new BufferedInputStream(fis);
			FileOutputStream fos=new FileOutputStream(target);
			BufferedOutputStream bos=new BufferedOutputStream(fos);
			int len=0;
			StringBuffer buufar=new StringBuffer();
			byte by[]=new byte[1024];
			while((len=bis.read(by))!=-1)
				bos.write(by);
			bos.flush();
			bis.close();
			bos.close();
		
	}

}

3、字符流

package com.java.copyFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyByFileReaderAndFileWriter {

	public static void main(String[] args) {
		long time1=System.currentTimeMillis();
		File source=new File("E:\\Java编程思想 第四版习题答案.pdf");
		File target=new File("F:\\Java编程思想 第四版习题答案.a");
		try {
			Copy( source, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time2=System.currentTimeMillis();
		System.out.println(time2-time1);
	}

	private static void Copy(File source, File target) throws IOException {
		if(target.exists())
			target.delete();
		FileReader fr=new FileReader(source);
		FileWriter fw=new FileWriter(target);
		char c[]=new char[1024];
		int len=0;
		while((len=fr.read(c))!=-1)
			fw.write(c);
		fr.close();
		fw.close();
		
	}

}

4、字符缓冲流

package com.java.copyFile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CopyByBufferReaderAndBufferWriter {

	public static void main(String[] args) {
		long time1=System.currentTimeMillis();
		File source=new File("E:\\Java编程思想 第四版习题答案.pdf");
		File target=new File("F:\\Java编程思想 第四版习题答案.a");
		try {
			Copy( source, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time2=System.currentTimeMillis();
		System.out.println(time2-time1);

	}

	private static void Copy(File source, File target) throws IOException {
		if(target.exists())
			target.delete();
		FileReader fr=new FileReader(source);
		BufferedReader br=new BufferedReader(fr);
		FileWriter fw=new FileWriter(target);
		BufferedWriter bw=new BufferedWriter(fw);
		String s=null; 
		StringBuffer buffer=new StringBuffer();
		while((s=br.readLine())!=null){
			buffer.append(s);
		}
		bw.write(new String(buffer));
		bw.flush();
		br.close();
		bw.close();
			
		
	}

}

5、单线程Channel

package com.java.copyFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

public class CopyByFileChannel {

	public static void main(String[] args) {
		long time1=System.currentTimeMillis();
//		File source=new File("E:\\Java编程思想 第四版习题答案.pdf");
//		File target=new File("F:\\Java编程思想 第四版习题答案.a");
		File source=new File("F:\\迅雷下载\\[电影天堂-www.dy2018.net]霸王别姬BD国语中字.rmvb");
		File target=new File("E:\\Java编程思想 第四版习题答案.a");
		try {
			Copy( source, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time2=System.currentTimeMillis();
		System.out.println(time2-time1);

	}

	private static void Copy(File source, File target) throws IOException {
		if(target.exists())
			target.delete();
		FileInputStream fis=new FileInputStream(source);
		FileOutputStream fos=new FileOutputStream(target);
		//input流调用方法getChannel()获得一个连接到底层文件的FileChannei对象(信道)
		FileChannel fileChannel=fis.getChannel();
		//WritableByetChannel(接口)可写入的字节通道
		WritableByteChannel writableByteChannel=fos.getChannel();
		//将字节从此通道的文件传输到给定的可写入字节通道。
		fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
		writableByteChannel.close();
		fileChannel.close();
		fos.close();
		fis.close();
		
	}

}

6、多线程channel

package com.java.copyFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

public class CopyFileThread extends Thread {
	private File source;
	private File target;
	private long start,end;
	public CopyFileThread(File source, File target, long start, long end) {
		this.source = source;
		this.target = target;
		this.start = start;
		this.end = end;
	}
	@Override
	public void run() {
		long starttime=System.currentTimeMillis();
		
		try {
			RandomAccessFile in=new RandomAccessFile(source, "r");
			RandomAccessFile out=new RandomAccessFile(target, "rw");
			in.seek(start);
			out.seek(start);
			FileChannel inChannel=in.getChannel();
			FileChannel outChanel=out.getChannel();
			//在可以锁定该区域、已关闭此通道或者已中断调用线程(以先到者为准)之前,将阻塞此方法的调用
			FileLock lock=outChanel.lock(start, (end-start), false);
			//试图读取从此通道的文件中给定 position 处开始的 count 个字节,并将其写入目标通道
			inChannel.transferTo(start, end-start, outChanel);
			lock.release();
			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long endtime=System.currentTimeMillis();
		System.out.println(Thread.currentThread().getName()+"  用时 "+(endtime-starttime));
	}
}

package com.java.copyFile;

import java.io.File;

public class Test {
	private  static int blockcount=1;
	public static void main(String[] args) {
		File source=new File("F:\\迅雷下载\\[电影天堂-www.dy2018.net]霸王别姬BD国语中字.rmvb");
		File target=new File("E:\\Java编程思想 第四版习题答案.a");
		if(target.exists())
			target.delete();
		long len=source.length();
		long oneNum=len/blockcount;
		for(int i=0;i


你可能感兴趣的:(java)