传统Java IO编写起来不仅代码繁琐,而且拷贝大文件时效率低下,使用NIO进行拷贝工作,代码简洁易读,而且读写速度非常快
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import org.junit.Test;
public class Copy {
@Test
public void copyFile()throws Exception{
FileInputStream fin=new FileInputStream("a.txt");
FileOutputStream fout =new FileOutputStream("z.txt");
FileChannel inChannel=fin.getChannel();
FileChannel outChannel=fout.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inChannel.close();
outChannel.close();
fin.close();
fout.close();
}
}