java实现文件拷贝的七种方式
1.通过字节流实现文件的拷贝
/**
*通过字节流实现文件的拷贝
*@paramsourcePath源文件路径
*@paramtargetPath目标文件路径
*/
publicstaticvoidcopyFileByStream(StringsourcePath,StringtargetPath){
//源文件路径
Filesource=newFile(sourcePath);
//目标文件路径
Filetarget=newFile(targetPath);
//如果源文件不存在则不能拷贝
if(!source.exists()){
return;
}
//如果目标文件目录不存在则创建
if(!target.getParentFile().exists()){
target.getParentFile().mkdirs();
}
try{
//实现文件的拷贝
InputStreaminputStream=newFileInputStream(source);
OutputStreamoutputStream=newFileOutputStream(target);
inttemp=0;
//每次读取1024个字节
byte[]data=newbyte[1024];
//将每次读取的数据保存到字节数组里面,并且返回读取的个数
while((temp=inputStream.read(data))!=-1){
//输出数组
outputStream.write(data,0,temp);
}
inputStream.close();
outputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
2.通过字符流实现文件拷贝
使用字符流只能拷贝文本文件
/**
*通过字符流实现文件的拷贝
*
*@paramsourcePath源文件路径
*@paramtargetPath目标文件路径
*/
publicstaticvoidcopyFileByReaderAndWriter(StringsourcePath,StringtargetPath){
//源文件路径
Filesource=newFile(sourcePath);
//目标文件路径
Filetarget=newFile(targetPath);
//如果源文件不存在则不能拷贝
if(!source.exists()){
return;
}
//如果目标文件目录不存在则创建
if(!target.getParentFile().exists()){
target.getParentFile().mkdirs();
}
FileReaderin=null;
FileWriterout=null;
try{
//字符输入流和字符输出流
in=newFileReader(source);
out=newFileWriter(target);
char[]c=newchar[1024];
inttemp=0;
//每次读取1024个字符
while((temp=in.read(c))!=-1){
//输出到文件
out.write(c,0,temp);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭流
try{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
3.通过字节缓冲流实现文件拷贝
/**
*通过字节缓冲流实现文件的拷贝
*
*@paramsourcePath源文件路径
*@paramtargetPath目标文件路径
*/
publicstaticvoidcopyFileByBuffered(StringsourcePath,StringtargetPath){
//源文件路径
Filesource=newFile(sourcePath);
//目标文件路径
Filetarget=newFile(targetPath);
//如果源文件不存在则不能拷贝
if(!source.exists()){
return;
}
//如果目标文件目录不存在则创建
if(!target.getParentFile().exists()){
target.getParentFile().mkdirs();
}
InputStreamin=null;
OutputStreamout=null;
try{
//字节缓冲输入流和字节缓冲输出流
in=newBufferedInputStream(newFileInputStream(source));
out=newBufferedOutputStream(newFileOutputStream(target));
byte[]b=newbyte[1024];
inttemp=0;
//每次读取一个1024的字节数组
while((temp=in.read(b))!=-1){
//输出到文件
out.write(b,0,temp);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
//关闭流
try{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
4.通过字符缓冲流拷贝文件
字符缓冲流只能读取文本文件
/**
*通过字符缓冲流实现文件的拷贝
*
*@paramsourcePath源文件路径
*@paramtargetPath目标文件路径
*/
publicstaticvoidcopyFileByBufferedChar(StringsourcePath,StringtargetPath){
//源文件路径
Filesource=newFile(sourcePath);
//目标文件路径
Filetarget=newFile(targetPath);
//如果源文件不存在则不能拷贝
if(!source.exists()){
return;
}
//如果目标文件目录不存在则创建
if(!target.getParentFile().exists()){
target.getParentFile().mkdirs();
}
BufferedReaderin=null;
BufferedWriterout=null;
try{
//字符缓冲输入流和字符缓冲输出流
in=newBufferedReader(newFileReader(source));
out=newBufferedWriter(newFileWriter(target));
//读取文件(每次读取一行)
Stringtemp=null;
while((temp=in.readLine())!=null){
//输出到文件
out.write(temp);
}
}catch(Exceptione){
e.printStackTrace();
}finally{
//关闭流
try{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
5.通过JAVANIO非直接缓冲区拷贝文件
/**
*通过JAVANIO非直接缓冲区拷贝文件
*
*@paramsourcePath源文件路径
*@paramtargetPath目标文件路径
*/
publicstaticvoidcopyFileByChannel(StringsourcePath,StringtargetPath){
FileChanneloutChannel=null;
FileChannelinChannel=null;
FileInputStreamfis=null;
FileOutputStreamfos=null;
try{
fis=newFileInputStream(sourcePath);
fos=newFileOutputStream(targetPath);
//获取通道
inChannel=fis.getChannel();
outChannel=fos.getChannel();
//分配指定大小的缓冲区
ByteBufferbuf=ByteBuffer.allocate(1024);
while(inChannel.read(buf)!=-1){
//转换为读取数据模式
buf.flip();
//写入到磁盘
outChannel.write(buf);
//清空缓冲区
buf.clear();
}
}catch(Exceptione){
e.printStackTrace();
}finally{
//关闭流
try{
if(outChannel!=null){
outChannel.close();
}
if(inChannel!=null){
inChannel.close();
}
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
6.通过JAVANIO直接缓冲区拷贝文件
/**
*通过JAVANIO直接缓冲区拷贝文件(内存映射文件)
*
*@paramsourcePath源文件路径
*@paramtargetPath目标文件路径
*/
publicstaticvoidcopyFileByChannelBufferd(StringsourcePath,StringtargetPath){
FileChannelinChannel=null;
FileChanneloutChannel=null;
try{
//获取通道,StandardOpenOption.READ表示可读,StandardOpenOption.WRITE表示可写,StandardOpenOption.CREATE表示可以创建
inChannel=FileChannel.open(Paths.get(sourcePath),StandardOpenOption.READ);
outChannel=FileChannel.open(Paths.get(targetPath),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);
//创建内存映射文件
MappedByteBufferinMapped=inChannel.map(FileChannel.MapMode.READ_ONLY,0,inChannel.size());
MappedByteBufferoutMapped=outChannel.map(FileChannel.MapMode.READ_WRITE,0,inChannel.size());
//直接操作内存映射文件
byte[]buf=newbyte[inMapped.limit()];
inMapped.get(buf);
outMapped.put(buf);
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭流
try{
if(outChannel!=null){
outChannel.close();
}
if(inChannel!=null){
inChannel.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
7.通过JAVANIO通道传输拷贝文件
方式一
/**
*通过JAVANIO通道传输拷贝文件
*
*@paramsourcePath源文件路径
*@paramtargetPath目标文件路径
*/
publicstaticvoidcopyFileByChannelTransfer(StringsourcePath,StringtargetPath){
FileChannelinChannel=null;
FileChanneloutChannel=null;
try{
//获取通道
inChannel=FileChannel.open(Paths.get(sourcePath),StandardOpenOption.READ);
outChannel=FileChannel.open(Paths.get(targetPath),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);
inChannel.transferTo(0,inChannel.size(),outChannel);
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭流
try{
if(outChannel!=null){
outChannel.close();
}
if(inChannel!=null){
inChannel.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
方式二
/**
*通过JAVANIO通道传输拷贝文件
*
*@paramsourcePath源文件路径
*@paramtargetPath目标文件路径
*/
publicstaticvoidcopyFileByChannelTransfer2(StringsourcePath,StringtargetPath){
FileInputStreamfis=null;
FileOutputStreamfos=null;
FileChannelinChannel=null;
FileChanneloutChannel=null;
try{
fis=newFileInputStream(sourcePath);
fos=newFileOutputStream(targetPath);
//获取通道
inChannel=fis.getChannel();
outChannel=fos.getChannel();
inChannel.transferTo(0,inChannel.size(),outChannel);
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭流
try{
if(outChannel!=null){
outChannel.close();
}
if(inChannel!=null){
inChannel.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
使用示例
Stringsource="e:\\demo\\纵天神帝.txt";
Stringtarget="e:\\demo\\";
longtime1=System.currentTimeMillis();
copyFileByStream(source,target+"1.txt");
System.out.println("通过字节流实现文件的拷贝耗时:"+(System.currentTimeMillis()-time1));
longtime2=System.currentTimeMillis();
copyFileByReaderAndWriter(source,target+"2.txt");
System.out.println("通过字符流实现文件的拷贝耗时:"+(System.currentTimeMillis()-time2));
longtime3=System.currentTimeMillis();
copyFileByBuffered(source,target+"3.txt");
System.out.println("通过字节缓冲流实现文件的拷贝耗时:"+(System.currentTimeMillis()-time3));
longtime4=System.currentTimeMillis();
copyFileByBufferedChar(source,target+"4.txt");
System.out.println("通过字符缓冲流实现文件的拷贝耗时:"+(System.currentTimeMillis()-time4));
longtime5=System.currentTimeMillis();
copyFileByChannel(source,target+"5.txt");
System.out.println("通过JAVANIO通道(非直接缓冲区)实现文件的拷贝耗时:"+(System.currentTimeMillis()-time5));
longtime6=System.currentTimeMillis();
copyFileByChannelBufferd(source,target+"6.txt");
System.out.println("通过JAVANIO通道(直接缓冲区)实现文件的拷贝耗时:"+(System.currentTimeMillis()-time6));
longtime7=System.currentTimeMillis();
copyFileByChannelTransfer(source,target+"7.txt");
System.out.println("通过JAVANIO通道传输实现文件的拷贝耗时:"+(System.currentTimeMillis()-time7));
longtime8=System.currentTimeMillis();
copyFileByChannelTransfer(source,target+"8.txt");
System.out.println("通过JAVANIO通道传输2实现文件的拷贝耗时:"+(System.currentTimeMillis()-time8));
通过测试发现,使用JAVANIO通道传输、JAVANIO通道直接缓冲区以及字节缓冲流拷贝文件效率最高
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。