一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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種方式及拷貝文件到另一個目錄下的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91精品国产综合久久消防器材 | 日本激情网 | 欧美日韩一区二区三区免费不卡 | 15同性同志18| 91欧美秘密入口 | 无码国产成人777爽死在线观看 | 亚洲AV无码专区国产乱码网站 | 亚洲国产成人久久精品影视 | 西西人体大胆啪啪私拍色约约 | 操黄| 亚洲男女网站 | 国模娜娜一区二区三区 | 黄动漫车车好快的车车双女主 | 欧美老人与小伙子性生交 | 91香蕉影院 | 6080欧美一区二区三区四区 | 四虎现在的网址入口2022 | 国产一区二区免费不卡在线播放 | 国产在线精品成人一区二区三区 | 青春草视频在线免费观看 | 天天狠天天天天透在线 | 亚洲AV蜜桃永久无码精品无码网 | 欧美日韩视频在线第一区二区三区 | 国内精品在线观看视频 | 亚洲精品在线网址 | 王雨纯羞羞 | 日本三级欧美三级人妇英文 | 国产精品久久久久久久午夜片 | 精品一卡2卡3卡4卡5卡亚洲 | katsumi精品hd | 午夜亚洲WWW湿好大 午夜想想爱 | a片毛片在线免费看 | 亚洲欧美日韩综合在线 | 久久这里只有精品国产精品99 | 国产在线视频在线观看 | 亚洲精品国产精麻豆久久99 | 久久99r66热这里有精品 | 国产探花在线观看 | 97视频久久 | 波多野结衣护士 | 亚洲福利在线观看 |