1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.transaction.util;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25
26
27
28
29
30
31
32 public final class FileHelper {
33
34 private static int BUF_SIZE = 50000;
35 private static byte[] BUF = new byte[BUF_SIZE];
36
37
38
39
40
41
42
43 public static boolean deleteFile(String path) {
44 File file = new File(path);
45 return file.delete();
46 }
47
48
49
50
51
52
53
54 public static boolean fileExists(String path) {
55 File file = new File(path);
56 return file.exists();
57 }
58
59
60
61
62
63
64
65
66
67 public static boolean createFile(String path) throws IOException {
68 File file = new File(path);
69 if (file.isDirectory()) {
70 return file.mkdirs();
71 } else {
72 File dir = file.getParentFile();
73
74 dir.mkdirs();
75 return file.createNewFile();
76 }
77 }
78
79
80
81
82
83
84
85 public static void removeRec(File toRemove) {
86 if (toRemove.isDirectory()) {
87 File fileList[] = toRemove.listFiles();
88 for (int a = 0; a < fileList.length; a++) {
89 removeRec(fileList[a]);
90 }
91 }
92 toRemove.delete();
93 }
94
95
96
97
98
99
100
101
102 public static void moveRec(File source, File target) throws IOException {
103 byte[] sharedBuffer = new byte[BUF_SIZE];
104 moveRec(source, target, sharedBuffer);
105 }
106
107 static void moveRec(File source, File target, byte[] sharedBuffer) throws IOException {
108 if (source.isDirectory()) {
109 if (!target.exists()) {
110 target.mkdirs();
111 }
112 if (target.isDirectory()) {
113
114 File[] files = source.listFiles();
115 for (int i = 0; i < files.length; i++) {
116 File file = files[i];
117 File targetFile = new File(target, file.getName());
118 if (file.isFile()) {
119 if (targetFile.exists()) {
120 targetFile.delete();
121 }
122 if (!file.renameTo(targetFile)) {
123 copy(file, targetFile, sharedBuffer);
124 file.delete();
125 }
126 } else {
127 if (!targetFile.exists()) {
128 if (!targetFile.mkdirs()) {
129 throw new IOException("Could not create target directory: "
130 + targetFile);
131 }
132 }
133 moveRec(file, targetFile);
134 }
135 }
136 source.delete();
137 }
138 } else {
139 if (!target.isDirectory()) {
140 copy(source, target, sharedBuffer);
141 source.delete();
142 }
143 }
144 }
145
146
147
148
149
150
151
152
153 public static void copyRec(File source, File target) throws IOException {
154 byte[] sharedBuffer = new byte[BUF_SIZE];
155 copyRec(source, target, sharedBuffer);
156 }
157
158 static void copyRec(File source, File target, byte[] sharedBuffer) throws IOException {
159 if (source.isDirectory()) {
160 if (!target.exists()) {
161 target.mkdirs();
162 }
163 if (target.isDirectory()) {
164
165 File[] files = source.listFiles();
166 for (int i = 0; i < files.length; i++) {
167 File file = files[i];
168 File targetFile = new File(target, file.getName());
169 if (file.isFile()) {
170 if (targetFile.exists()) {
171 targetFile.delete();
172 }
173 copy(file, targetFile, sharedBuffer);
174 } else {
175 targetFile.mkdirs();
176 copyRec(file, targetFile);
177 }
178 }
179 }
180 } else {
181 if (!target.isDirectory()) {
182 if (!target.exists()) {
183 File dir = target.getParentFile();
184 if(!dir.exists() && !dir.mkdirs()) {
185 throw new IOException("Could not create target directory: " + dir);
186 }
187 if (!target.createNewFile()) {
188 throw new IOException("Could not create target file: " + target);
189 }
190 }
191 copy(source, target, sharedBuffer);
192 }
193 }
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208 public static long copy(File input, File output) throws IOException {
209 FileInputStream in = null;
210 try {
211 in = new FileInputStream(input);
212 return copy(in, output);
213 } finally {
214 if (in != null) {
215 try {
216 in.close();
217 } catch (IOException e) {
218 }
219 }
220 }
221 }
222
223
224
225
226
227
228
229
230
231
232
233 public static long copy(File input, File output, byte[] copyBuffer) throws IOException {
234 FileInputStream in = null;
235 FileOutputStream out = null;
236 try {
237 in = new FileInputStream(input);
238 out = new FileOutputStream(output);
239 return copy(in, out, copyBuffer);
240 } finally {
241 if (in != null) {
242 try {
243 in.close();
244 } catch (IOException e) {
245 }
246 }
247 if (out != null) {
248 try {
249 out.close();
250 } catch (IOException e) {
251 }
252 }
253 }
254 }
255
256
257
258
259
260
261
262
263
264
265 public static long copy(InputStream in, File outputFile) throws IOException {
266 FileOutputStream out = null;
267 try {
268 out = new FileOutputStream(outputFile);
269 return copy(in, out);
270 } finally {
271 if (out != null) {
272 try {
273 out.close();
274 } catch (IOException e) {
275 }
276 }
277 }
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291 public static long copy(InputStream in, OutputStream out) throws IOException {
292
293 byte[] buf = new byte[BUF_SIZE];
294 return copy(in, out, buf);
295 }
296
297
298
299
300
301
302
303
304
305
306
307
308 public static long globalBufferCopy(InputStream in, OutputStream out) throws IOException {
309 synchronized (BUF) {
310 return copy(in, out, BUF);
311 }
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325 public static long copy(InputStream in, OutputStream out, byte[] copyBuffer) throws IOException {
326 long bytesCopied = 0;
327 int read = -1;
328
329 while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) {
330 out.write(copyBuffer, 0, read);
331 bytesCopied += read;
332 }
333 return bytesCopied;
334 }
335 }