服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java复制文件的4种方式及拷贝文件到另一个目录下的实例代码

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

2021-05-11 14:06Java教程网 Java教程

这篇文章主要介绍了java复制文件的4种方式,通过实例带给大家介绍了java 拷贝文件到另一个目录下的方法,需要的朋友可以参考下

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

1. 使用filestreams复制

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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方法,根据文档应该比文件流复制的速度更快。 这是第二种方法的代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
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内部。 这是第三种方法的代码:

?
1
2
3
4
private static void copyfileusingapachecommonsio(file source, file dest)
    throws ioexception {
  fileutils.copyfile(source, dest);
}

4. 使用java7的files类复制

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

?
1
2
3
4
private static void copyfileusingjava7files(file source, file dest)
    throws ioexception {  
    files.copy(source.topath(), dest.topath());
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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种方式及拷贝文件到另一个目录下的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

延伸 · 阅读

精彩推荐