상용 파일 복사 방법

8222 단어 자바
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. 단일 스 레 드 채널
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. 다 중 스 레 드 채널
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

좋은 웹페이지 즐겨찾기