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

2025-05-29 0 62

尽管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种方式及拷贝文件到另一个目录下的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java复制文件的4种方式及拷贝文件到另一个目录下的实例代码 https://www.kuaiidc.com/111461.html

相关文章

发表评论
暂无评论