在使用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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
package com.wangpeng.utill; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.PrintWriter; /** * @author wangpeng * */ public class ToolOfFile { /** * 創建目錄 * * @param folderPath * 目錄目錄 * @throws Exception */ public static void newFolder(String folderPath) throws Exception { try { java.io.File myFolder = new java.io.File(folderPath); if (!myFolder.exists()) { myFolder.mkdir(); } } catch (Exception e) { throw e; } } /** * 創建文件 * * @param filePath * 文件路徑 * @throws Exception */ public static void newFile(String filePath) throws Exception { try { File myFile = new File(filePath); if (!myFile.exists()) { myFile.createNewFile(); } } catch (Exception e) { throw e; } } /** * 創建文件,并寫入內容 * * @param filePath * 文件路徑 * @param fileContent * 被寫入的文件內容 * @throws Exception */ public static void newFile(String filePath, String fileContent) throws Exception { // 用來寫入字符文件的便捷類 FileWriter fileWriter = null ; // 向文本輸出流打印對象的格式化表示形式,使用指定文件創建不具有自己主動行刷新的新 PrintWriter printWriter = null ; try { File myFile = new File(filePath); if (!myFile.exists()) { myFile.createNewFile(); } fileWriter = new FileWriter(myFile); printWriter = new PrintWriter(fileWriter); printWriter.print(fileContent); printWriter.flush(); } catch (Exception e) { throw e; } finally { if (printWriter != null ) { printWriter.close(); } if (fileWriter != null ) { fileWriter.close(); } } } /** * 復制文件 * * @param oldPath * 被拷貝的文件 * @param newPath * 復制到的文件 * @throws Exception */ public static void copyFile(String oldPath, String newPath) throws Exception { InputStream inStream = null ; FileOutputStream outStream = null ; try { int byteread = 0 ; File oldfile = new File(oldPath); // 文件存在時 if (oldfile.exists()) { inStream = new FileInputStream(oldfile); outStream = new FileOutputStream(newPath); byte [] buffer = new byte [ 1444 ]; while ((byteread = inStream.read(buffer)) != - 1 ) { outStream.write(buffer, 0 , byteread); } outStream.flush(); } } catch (Exception e) { throw e; } finally { if (outStream != null ) { outStream.close(); } if (inStream != null ) { inStream.close(); } } } /** * 復制文件 * @param inStream 被拷貝的文件的輸入流 * @param newPath 被復制到的目標 * @throws Exception */ public static void copyFile(InputStream inStream, String newPath) throws Exception { FileOutputStream outStream = null ; try { int byteread = 0 ; outStream = new FileOutputStream(newPath); byte [] buffer = new byte [ 1444 ]; while ((byteread = inStream.read(buffer)) != - 1 ) { outStream.write(buffer, 0 , byteread); } outStream.flush(); } catch (Exception e) { throw e; } finally { if (outStream != null ) { outStream.close(); } if (inStream != null ) { inStream.close(); } } } /** * 復制目錄 * * @param oldPath * 被復制的目錄路徑 * @param newPath * 被復制到的目錄路徑 * @throws Exception */ public static void copyFolder(String oldPath, String newPath) throws Exception { try { ( new File(newPath)).mkdirs(); // 假設目錄不存在 則建立新目錄 File a = new File(oldPath); String[] file = a.list(); File tempIn = null ; for ( int i = 0 ; i < file.length; i++) { if (oldPath.endsWith(File.separator)) { tempIn = new File(oldPath + file[i]); } else { tempIn = new File(oldPath + File.separator + file[i]); } if (tempIn.isFile()) { copyFile(tempIn.getAbsolutePath(), newPath + "/" + (tempIn.getName()).toString()); } else if (tempIn.isDirectory()) { // 假設是子目錄 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { throw e; } } /** * 刪除文件 * * @param filePathAndName */ public static void delFileX(String filePathAndName) { File myDelFile = new File(filePathAndName); myDelFile.delete(); } /** * 刪除目錄 * * @param path */ public static void delForder(String path) { File delDir = new File(path); if (!delDir.exists()) { return ; } if (!delDir.isDirectory()) { return ; } String[] tempList = delDir.list(); File temp = null ; for ( int i = 0 ; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } else if (temp.isDirectory()) { // 刪除完里面全部內容 delForder(path + "/" + tempList[i]); } } delDir.delete(); } public static void main(String[] args) { String oldPath = "F:/test/aaa/" ; String newPath = "F:/test/bbb/" ; try { // ToolOfFile.newFolder("F:/test/hello/"); // ToolOfFile.newFile("F:/test/hello/world.txt","我愛你,the world! "); ToolOfFile.copyFolder(oldPath, newPath); // ToolOfFile.delForder("F:/test/hello"); } catch (Exception e) { e.printStackTrace(); } System.out.println( "OK" ); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。