1 28 29 package org.apache.commons.transaction.util; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 38 44 public final class FileHelper { 45 46 private static int BUF_SIZE = 50000; 47 private static byte[] BUF = new byte[BUF_SIZE]; 48 49 55 public static boolean deleteFile(String path) { 56 File file = new File (path); 57 return file.delete(); 58 } 59 60 66 public static boolean fileExists(String path) { 67 File file = new File (path); 68 return file.exists(); 69 } 70 71 79 public static boolean createFile(String path) throws IOException { 80 File file = new File (path); 81 if (file.isDirectory()) { 82 return file.mkdirs(); 83 } else { 84 File dir = file.getParentFile(); 85 dir.mkdirs(); 87 return file.createNewFile(); 88 } 89 } 90 91 97 public static void removeRec(File toRemove) { 98 if (toRemove.isDirectory()) { 99 File fileList[] = toRemove.listFiles(); 100 for (int a = 0; a < fileList.length; a++) { 101 removeRec(fileList[a]); 102 } 103 } 104 toRemove.delete(); 105 } 106 107 114 public static void moveRec(File source, File target) throws IOException { 115 byte[] sharedBuffer = new byte[BUF_SIZE]; 116 moveRec(source, target, sharedBuffer); 117 } 118 119 static void moveRec(File source, File target, byte[] sharedBuffer) throws IOException { 120 if (source.isDirectory()) { 121 if (!target.exists()) { 122 target.mkdirs(); 123 } 124 if (target.isDirectory()) { 125 126 File [] files = source.listFiles(); 127 for (int i = 0; i < files.length; i++) { 128 File file = files[i]; 129 File targetFile = new File (target, file.getName()); 130 if (file.isFile()) { 131 if (targetFile.exists()) { 132 targetFile.delete(); 133 } 134 if (!file.renameTo(targetFile)) { 135 copy(file, targetFile, sharedBuffer); 136 file.delete(); 137 } 138 } else { 139 targetFile.mkdirs(); 140 moveRec(file, targetFile); 141 } 142 } 143 source.delete(); 144 } 145 } else { 146 if (!target.isDirectory()) { 147 copy(source, target, sharedBuffer); 148 source.delete(); 149 } 150 } 151 } 152 153 160 public static void copyRec(File source, File target) throws IOException { 161 byte[] sharedBuffer = new byte[BUF_SIZE]; 162 copyRec(source, target, sharedBuffer); 163 } 164 165 static void copyRec(File source, File target, byte[] sharedBuffer) throws IOException { 166 if (source.isDirectory()) { 167 if (!target.exists()) { 168 target.mkdirs(); 169 } 170 if (target.isDirectory()) { 171 172 File [] files = source.listFiles(); 173 for (int i = 0; i < files.length; i++) { 174 File file = files[i]; 175 File targetFile = new File (target, file.getName()); 176 if (file.isFile()) { 177 if (targetFile.exists()) { 178 targetFile.delete(); 179 } 180 copy(file, targetFile, sharedBuffer); 181 } else { 182 targetFile.mkdirs(); 183 copyRec(file, targetFile); 184 } 185 } 186 } 187 } else { 188 if (!target.isDirectory()) { 189 if (!target.exists()) { 190 target.getParentFile().mkdirs(); 191 target.createNewFile(); 192 } 193 copy(source, target, sharedBuffer); 194 } 195 } 196 } 197 198 210 public static long copy(File input, File output) throws IOException { 211 FileInputStream in = null; 212 try { 213 in = new FileInputStream (input); 214 return copy(in, output); 215 } finally { 216 if (in != null) { 217 try { 218 in.close(); 219 } catch (IOException e) { 220 } 221 } 222 } 223 } 224 225 235 public static long copy(File input, File output, byte[] copyBuffer) throws IOException { 236 FileInputStream in = null; 237 FileOutputStream out = null; 238 try { 239 in = new FileInputStream (input); 240 out = new FileOutputStream (output); 241 return copy(in, out, copyBuffer); 242 } finally { 243 if (in != null) { 244 try { 245 in.close(); 246 } catch (IOException e) { 247 } 248 } 249 if (out != null) { 250 try { 251 out.close(); 252 } catch (IOException e) { 253 } 254 } 255 } 256 } 257 258 267 public static long copy(InputStream in, File outputFile) throws IOException { 268 FileOutputStream out = null; 269 try { 270 out = new FileOutputStream (outputFile); 271 return copy(in, out); 272 } finally { 273 if (out != null) { 274 try { 275 out.close(); 276 } catch (IOException e) { 277 } 278 } 279 } 280 } 281 282 293 public static long copy(InputStream in, OutputStream out) throws IOException { 294 byte[] buf = new byte[BUF_SIZE]; 296 return copy(in, out, buf); 297 } 298 299 310 public static long globalBufferCopy(InputStream in, OutputStream out) throws IOException { 311 synchronized (BUF) { 312 return copy(in, out, BUF); 313 } 314 } 315 316 327 public static long copy(InputStream in, OutputStream out, byte[] copyBuffer) throws IOException { 328 long bytesCopied = 0; 329 int read = -1; 330 331 while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) { 332 out.write(copyBuffer, 0, read); 333 bytesCopied += read; 334 } 335 return bytesCopied; 336 } 337 } 338 | Popular Tags |