01 | //相應的路徑需要添加權限支持,各位看自己需要操作的檔案添加對應的權限了 |
02 |
03 | package c ommon; |
04 | import java.io.File; |
05 | import java.io.FileInputStream; |
06 | import java.io.FileOutputStream; |
07 | import java.io.InputStream; |
08 |
09 |
10 |
11 |
12 | /** |
13 | * 複製單個檔案 |
14 | * @param oldPath String 原檔案路徑如:c:/fqf.txt |
15 | * @param newPath String 複製後路徑如:f:/fqf.txt |
16 | * @return boolean |
17 | */ |
18 | public boolean copyFile(String oldPath, String newPath) { |
19 | boolean isok = true ; |
20 | try { |
21 | int bytesum = 0 ; |
22 | int byteread = 0 ; |
23 | File oldfile = new File(oldPath); |
24 | if (oldfile.exists()) { //檔案存在時 |
25 | InputStream inStream = new FileInputStream(oldPath); //讀入原檔案 |
26 | FileOutputStream fs = new FileOutputStream(newPath); |
27 | byte [] buffer = new byte [ 1024 ]; |
28 | int length; |
29 | while ( (byteread = inStream.read(buffer)) != - 1 ) { |
30 | bytesum += byteread; //byte數檔案大小 |
31 | //System.out.println(bytesum); |
32 | fs.write(buffer, 0 , byteread); |
33 | } |
34 | fs.flush(); |
35 | fs.close(); |
36 | inStream.close(); |
37 | } |
38 | else |
39 | { |
40 | isok = false ; |
41 | } |
42 | } |
43 | catch (Exception e) { |
44 | // System.out.println("複製單個檔案操作出錯"); |
45 | // e.printStackTrace(); |
46 | isok = false ; |
47 | } |
48 | return isok; |
49 |
50 | } |
51 |
52 | /** |
53 | * 複製整個資料夾內容 |
54 | * @param oldPath String 原檔案徑如:c:/fqf |
55 | * @param newPath String 複製後路徑如:f:/fqf/ff |
56 | * @return boolean |
57 | */ |
58 | public boolean copyFolder(String oldPath, String newPath) { |
59 | boolean isok = true ; |
60 | try { |
61 | ( new File(newPath)).mkdirs(); //如果資料夾不存在則建立資料夾 |
62 | File a= new File(oldPath); |
63 | String[] file=a.list(); |
64 | File temp= null ; |
65 | for ( int i = 0 ; i < file.length; i++) { |
66 | if (oldPath.endsWith(File.separator)){ |
67 | temp= new File(oldPath+file[i]); |
68 | } |
69 | else |
70 | { |
71 | temp= new File(oldPath+File.separator+file[i]); |
72 | } |
73 |
74 | if (temp.isFile()){ |
75 | FileInputStream input = new FileInputStream(temp); |
76 | FileOutputStream output = new FileOutputStream(newPath + "/" + |
77 | (temp.getName()).toString()); |
78 | byte [] b = new byte [ 1024 * 5 ]; |
79 | int len; |
80 | while ( (len = input.read(b)) != - 1 ) { |
81 | output.write(b, 0 , len); |
82 | } |
83 | output.flush(); |
84 | output.close(); |
85 | input.close(); |
86 | } |
87 | if (temp.isDirectory()){ //如果是子資料夾 |
88 | copyFolder(oldPath+ "/" +file[i],newPath+ "/" +file[i]); |
89 | } |
90 | } |
91 | } |
92 | catch (Exception e) { |
93 | isok = false ; |
94 | } |
95 | return isok; |
96 | } |
請問
回覆刪除public void copy(String comePath,String goPath) {..}
中,他說沒有找到copy()這個動作
已經嘗試用
public void copy(String comePath,String goPath,View v) {..}
但是一樣找不到