当前位置: 移动技术网 > IT编程>开发语言>Java > java复制文件的4种方式及拷贝文件到另一个目录下的实例代码

java复制文件的4种方式及拷贝文件到另一个目录下的实例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

尽管java提供了一个可以处理文件的io操作类。 但是没有一个复制文件的方法。 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候。 然而有几种方法可以进行java文件复制操作,下面列举出4中最受欢迎的方式。

1. 使用filestreams复制

这是最经典的方式将一个文件的内容复制到另一个文件中。 使用fileinputstream读取文件a的字节,使用fileoutputstream写入到文件b。 这是第一个方法的代码:

private static void copyfileusingfilestreams(file source, file dest) 
    throws ioexception {   
  inputstream input = null;   
  outputstream output = null;   
  try { 
      input = new fileinputstream(source); 
      output = new fileoutputstream(dest);     
      byte[] buf = new byte[1024];     
      int bytesread;     
      while ((bytesread = input.read(buf)) > 0) { 
        output.write(buf, 0, bytesread); 
      } 
  } finally { 
    input.close(); 
    output.close(); 
  } 
} 

正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。

2. 使用filechannel复制

java nio包括transferfrom方法,根据文档应该比文件流复制的速度更快。 这是第二种方法的代码:

private static void copyfileusingfilechannels(file source, file dest) throws ioexception {   
    filechannel inputchannel = null;   
    filechannel outputchannel = null;   
  try { 
    inputchannel = new fileinputstream(source).getchannel(); 
    outputchannel = new fileoutputstream(dest).getchannel(); 
    outputchannel.transferfrom(inputchannel, 0, inputchannel.size()); 
  } finally { 
    inputchannel.close(); 
    outputchannel.close(); 
  } 
} 

3. 使用commons io复制

apache commons io提供拷贝文件方法在其fileutils类,可用于复制一个文件到另一个地方。它非常方便使用apache commons fileutils类时,您已经使用您的项目。基本上,这个类使用java nio filechannel内部。 这是第三种方法的代码:

private static void copyfileusingapachecommonsio(file source, file dest) 
    throws ioexception { 
  fileutils.copyfile(source, dest); 
} 

4. 使用java7的files类复制

如果你有一些经验在java 7中你可能会知道,可以使用复制方法的files类文件,从一个文件复制到另一个文件。 这是第四个方法的代码:

private static void copyfileusingjava7files(file source, file dest) 
    throws ioexception {   
    files.copy(source.topath(), dest.topath()); 
}

下面看下java拷贝文件到另一个目录下的实现代码,具体代码如下所示:

package com.util;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
public class testhtml {
/** 
* 复制单个文件 
* @param oldpath string 原文件路径 如:c:/fqf.txt 
* @param newpath string 复制后路径 如:f:/fqf.txt 
* @return boolean 
*/ 
public void copyfile(string oldpath, string newpath) { 
try { 
int bytesum = 0; 
int byteread = 0; 
file oldfile = new file(oldpath); 
if (oldfile.exists()) { //文件存在时 
inputstream instream = new fileinputstream(oldpath); //读入原文件 
fileoutputstream fs = new fileoutputstream(newpath); 
byte[] buffer = new byte[1444]; 
int length; 
while ( (byteread = instream.read(buffer)) != -1) { 
bytesum += byteread; //字节数 文件大小 
system.out.println(bytesum); 
fs.write(buffer, 0, byteread); 
} 
instream.close(); 
} 
} 
catch (exception e) { 
system.out.println("复制单个文件操作出错"); 
e.printstacktrace();
}
}
/** 
* 复制整个文件夹内容 
* @param oldpath string 原文件路径 如:c:/fqf 
* @param newpath string 复制后路径 如:f:/fqf/ff 
* @return boolean 
*/ 
public void copyfolder(string oldpath, string newpath) {
try { 
(new file(newpath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 
file a=new file(oldpath); 
string[] file=a.list(); 
file temp=null; 
for (int i = 0; i < file.length; i++) { 
if(oldpath.endswith(file.separator)){ 
temp=new file(oldpath+file[i]); 
} 
else{ 
temp=new file(oldpath+file.separator+file[i]); 
}
if(temp.isfile()){ 
fileinputstream input = new fileinputstream(temp); 
fileoutputstream output = new fileoutputstream(newpath + "/" + 
(temp.getname()).tostring()); 
byte[] b = new byte[1024 * 5]; 
int len; 
while ( (len = input.read(b)) != -1) { 
output.write(b, 0, len); 
} 
output.flush(); 
output.close(); 
input.close(); 
} 
if(temp.isdirectory()){//如果是子文件夹 
copyfolder(oldpath+"/"+file[i],newpath+"/"+file[i]); 
} 
} 
} 
catch (exception e) { 
system.out.println("复制整个文件夹内容操作出错"); 
e.printstacktrace();
}
}
public static void main(string[] args)throws exception {
// //这是你的源文件,本身是存在的
// file beforefile = new file("c:/users/administrator/desktop/untitled-2.html");
//
// //这是你要保存之后的文件,是自定义的,本身不存在
// file afterfile = new file("c:/users/administrator/desktop/jiekou0/untitled-2.html");
//
// //定义文件输入流,用来读取beforefile文件
// fileinputstream fis = new fileinputstream(beforefile);
//
// //定义文件输出流,用来把信息写入afterfile文件中
// fileoutputstream fos = new fileoutputstream(afterfile);
//
// //文件缓存区
// byte[] b = new byte[1024];
// //将文件流信息读取文件缓存区,如果读取结果不为-1就代表文件没有读取完毕,反之已经读取完毕
// while(fis.read(b)!=-1){
// //将缓存区中的内容写到afterfile文件中
// fos.write(b);
// fos.flush();
// }
string oldpath="c:/users/administrator/desktop/untitled-2.html";
string newpath="c:/users/administrator/desktop/jiekou0/untitled-2.html";
testhtml t=new testhtml();
t.copyfile(oldpath, newpath);
}
}

总结

以上所述是小编给大家介绍的java复制文件的4种方式及拷贝文件到另一个目录下的实例代码,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网