文件拷贝
使用Io操作进行文件拷贝
想要更好的体验,我们需要找到一个.mp4文件作为目标文件
这里一共有六种实现方法,大家可以试一试,看看哪一个更快。
package com.io1;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class TestFileCopy {
public static void main(String[] args) throws Exception{
fileCopy2("F:\\a.mp4","F:\\b.mp4");
fileCopy3("F:\\a.mp4","F:\\c.mp4");
fileCopy4("F:\\a.mp4","F:\\d.mp4");
fileCopy5("F:\\a.mp4","F:\\e.mp4");
fileCopy6("F:\\a.mp4","F:\\f.mp4");
}
//方法一
static void fileCopy1(String srcName,String destName) throws Exception{//一个字节一个字节读写,太慢,有时间可以等,反正被我淘汰。
FileInputStream fis = new FileInputStream(srcName);
FileOutputStream fos = new FileOutputStream(destName);
while(true){
int a = fis.read();
if (a == -1) break;
fos.write(a);
}
fis.close();
fos.close();
}
//方法二
static void fileCopy2(String srcName,String destName) throws Exception{//一次拷贝一个字节数组
long t1 = System.nanoTime();
FileInputStream fis = new FileInputStream(srcName);
FileOutputStream fos = new FileOutputStream(destName);
byte[] bs = new byte[1024];
while(true){
int len = fis.read(bs);
if (len == -1) break;
// fos.write(bs);//如果用这个请对比两个文件属性,查看字节数,发现不一样
fos.write(bs, 0 , len);
}
fis.close();
fos.close();
long t2 = System.nanoTime();
System.out.println((t2-t1)/1E9);
}
//方法三
static void fileCopy3(String srcName,String destName) throws Exception{//不仅拷字节数组,还加了一个缓冲区
long t1 = System.nanoTime();
FileInputStream fis = new FileInputStream(srcName);
BufferedInputStream in = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(destName);
BufferedOutputStream out = new BufferedOutputStream(fos);
byte[] bs = new byte[1024];
while(true){
int len = in.read(bs);
if (len == -1) break;
out.write(bs, 0 , len);
}
in.close();
out.close();
long t2 = System.nanoTime();
System.out.println((t2-t1)/1E9);
}
//使用通道解决
//方法四
static void fileCopy4(String srcName,String destName) throws Exception{
long t1 = System.nanoTime();
ByteBuffer buffer = ByteBuffer.allocate(1024);//ByteBuffer.allocate(容量) ByteBuffer.wrap(byte[])
FileInputStream fis = new FileInputStream(srcName);
FileOutputStream fos = new FileOutputStream(destName);
FileChannel channel1 = fis.getChannel();
FileChannel channel2 = fos.getChannel();
while(true){
int a = channel1.read(buffer);
if (a == -1) break;
/*参考Buffer API
position:当前读写位置
limit:限制
capacity:容量
flip():position重置为0,即从写模式到读模式
clear(): position、limit重置为0,从读模式到写模式,
*/
buffer.flip();//写模式->读模式
channel2.write(buffer);
buffer.clear();//读模式->写模式。清空,准备下一次
}
channel1.close();
channel2.close();
long t2 = System.nanoTime();
System.out.println((t2-t1)/1E9);
}
//方法五
static void fileCopy5(String srcName,String destName) throws Exception{
long t1 = System.nanoTime();
FileInputStream fis = new FileInputStream(srcName);
FileOutputStream fos = new FileOutputStream(destName);
FileChannel channel1 = fis.getChannel();
FileChannel channel2 = fos.getChannel();
MappedByteBuffer buffer = channel1.map(FileChannel.MapMode.READ_ONLY, 0, channel1.size());
channel2.write(buffer);
channel1.close();
channel2.close();
long t2 = System.nanoTime();
System.out.println((t2-t1)/1E9);
}
//方法六
static void fileCopy6(String srcName,String destName) throws Exception{
long t1 = System.nanoTime();
FileInputStream fis = new FileInputStream(srcName);
FileOutputStream fos = new FileOutputStream(destName);
FileChannel channel1 = fis.getChannel();
FileChannel channel2 = fos.getChannel();
channel1.transferTo(0, channel1.size(), channel2);
channel1.close();
channel2.close();
long t2 = System.nanoTime();
System.out.println((t2-t1)/1E9);
}
}