传统IO流与NIO流的文件4种拷贝方式

package com.nio;java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;web

public class TestBuffer {
public static void main(String[] args) {数组

}
//NIO使用工具类Files直接copyFile
public static void copyFile3(String inputFile,String outputFile)  {		
	try {
		//copy文件
		Files.copy(Paths.get(inputFile),new FileOutputStream(new File(outputFile)));
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}							
}
//NIO直接缓冲区(内存映射)  占用系统资源过多,不太稳定
public static void copyFile2(String inputFile,String outputFile){
	MappedByteBuffer mapIn=null;
	MappedByteBuffer mapOut=null;
	FileChannel in=null;
	FileChannel out=null;
	try {
		//建立输入管道(只读)
		 in = FileChannel.open(Paths.get(inputFile), StandardOpenOption.READ);
		 //建立输出管道(可读可写每次会覆盖原有文件)
		 out = FileChannel.open(Paths.get(outputFile), StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);
		//建立内存映射文件
		 mapIn = in.map(MapMode.READ_ONLY, 0, in.size());
		mapOut = out.map(MapMode.READ_WRITE, 0, in.size());
		//读到数组中
		byte [] b = new byte[mapIn.limit()];
		//进行读的操做
		mapIn.get(b);
		//进行写的操做
		mapOut.put(b);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		if(in!=null) {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(out!=null) {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
			
}
//NIO无直接缓冲区
public static void copyFile1(File in,File out){
	FileInputStream fi=null;
	FileOutputStream fo=null;
	FileChannel fcin=null;
	FileChannel fcout=null;
	try {
		//建立输入输出流对象
		fi = new FileInputStream(in);
		fo = new FileOutputStream(out);
		//获取管道
		fcin = fi.getChannel();
		fcout = fo.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		//读取到缓冲区
		while(fcin.read(buffer)!=-1) {
			//翻转模式
			buffer.flip();
			//从缓冲区取数据进行写入操做
			fcout.write(buffer);
			//清空缓冲区
			buffer.clear();
		}
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		if(fi!=null) {
			try {
				fi.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(fo!=null) {
			try {
				fo.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(fcout!=null) {
			try {
				fcout.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(fcin!=null) {
			try {
				fcin.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	
	}				
}
//传统IO文件拷贝
public static void coytFile(){
	InputStream in=null;
	OutputStream out=null;
	try {
		 in = new FileInputStream(new File("a.text"));
		 out = new FileOutputStream(new File("b.txt"));
		byte [] str = new byte[1024];
		int len=-1;
		while((len=in.read(str))!=-1){
			out.write(str, 0, len);
			out.flush();
			
		}
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
		if(in!=null) {
			try {
				in.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(out!=null) {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

}app