1 23 24 package org.apache.slide.store.util; 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.OutputStream ; 32 33 38 public final class FileHelper { 39 40 private static int BUF_SIZE = 50000; 41 private static byte[] BUF = new byte[BUF_SIZE]; 42 43 49 public static boolean deleteFile(String path) { 50 File file = new File (path); 51 return file.delete(); 52 } 53 54 60 public static boolean fileExists(String path) { 61 File file = new File (path); 62 return file.exists(); 63 } 64 65 73 public static boolean createFile(String path) throws IOException { 74 File file = new File (path); 75 if (file.isDirectory()) { 76 return file.mkdirs(); 77 } else { 78 File dir = file.getParentFile(); 79 dir.mkdirs(); 81 return file.createNewFile(); 82 } 83 } 84 85 91 public static void removeRec(File toRemove) { 92 if (toRemove.isDirectory()) { 93 File fileList[] = toRemove.listFiles(); 94 for (int a = 0; a < fileList.length; a++) { 95 removeRec(fileList[a]); 96 } 97 } 98 toRemove.delete(); 99 } 100 101 108 public static void moveRec(File source, File target) throws IOException { 109 byte[] sharedBuffer = new byte[BUF_SIZE]; 110 moveRec(source, target, sharedBuffer); 111 } 112 113 static void moveRec(File source, File target, byte[] sharedBuffer) throws IOException { 114 if (source.isDirectory()) { 115 if (!target.exists()) { 116 target.mkdirs(); 117 } 118 if (target.isDirectory()) { 119 120 File [] files = source.listFiles(); 121 for (int i = 0; i < files.length; i++) { 122 File file = files[i]; 123 File targetFile = new File (target, file.getName()); 124 if (file.isFile()) { 125 if (targetFile.exists()) { 126 targetFile.delete(); 127 } 128 if (!file.renameTo(targetFile)) { 129 copy(file, targetFile, sharedBuffer); 130 file.delete(); 131 } 132 } else { 133 targetFile.mkdirs(); 134 moveRec(file, targetFile); 135 } 136 } 137 source.delete(); 138 } 139 } else { 140 if (!target.isDirectory()) { 141 copy(source, target, sharedBuffer); 142 source.delete(); 143 } 144 } 145 } 146 147 154 public static void copyRec(File source, File target) throws IOException { 155 byte[] sharedBuffer = new byte[BUF_SIZE]; 156 copyRec(source, target, sharedBuffer); 157 } 158 159 static void copyRec(File source, File target, byte[] sharedBuffer) throws IOException { 160 if (source.isDirectory()) { 161 if (!target.exists()) { 162 target.mkdirs(); 163 } 164 if (target.isDirectory()) { 165 166 File [] files = source.listFiles(); 167 for (int i = 0; i < files.length; i++) { 168 File file = files[i]; 169 File targetFile = new File (target, file.getName()); 170 if (file.isFile()) { 171 if (targetFile.exists()) { 172 targetFile.delete(); 173 } 174 copy(file, targetFile, sharedBuffer); 175 } else { 176 targetFile.mkdirs(); 177 copyRec(file, targetFile); 178 } 179 } 180 } 181 } else { 182 if (!target.isDirectory()) { 183 copy(source, target, sharedBuffer); 184 } 185 } 186 } 187 188 200 public static long copy(File input, File output) throws IOException { 201 FileInputStream in = null; 202 try { 203 in = new FileInputStream (input); 204 return copy(in, output); 205 } finally { 206 if (in != null) { 207 try { 208 in.close(); 209 } catch (IOException e) { 210 } 211 } 212 } 213 } 214 215 225 public static long copy(File input, File output, byte[] copyBuffer) throws IOException { 226 FileInputStream in = null; 227 FileOutputStream out = null; 228 try { 229 in = new FileInputStream (input); 230 out = new FileOutputStream (output); 231 return copy(in, out, copyBuffer); 232 } finally { 233 if (in != null) { 234 try { 235 in.close(); 236 } catch (IOException e) { 237 } 238 } 239 if (out != null) { 240 try { 241 out.close(); 242 } catch (IOException e) { 243 } 244 } 245 } 246 } 247 248 257 public static long copy(InputStream in, File outputFile) throws IOException { 258 FileOutputStream out = null; 259 try { 260 out = new FileOutputStream (outputFile); 261 return copy(in, out); 262 } finally { 263 if (out != null) { 264 try { 265 out.close(); 266 } catch (IOException e) { 267 } 268 } 269 } 270 } 271 272 283 public static long copy(InputStream in, OutputStream out) throws IOException { 284 byte[] buf = new byte[BUF_SIZE]; 286 return copy(in, out, buf); 287 } 288 289 300 public static long globalBufferCopy(InputStream in, OutputStream out) throws IOException { 301 synchronized (BUF) { 302 return copy(in, out, BUF); 303 } 304 } 305 306 317 public static long copy(InputStream in, OutputStream out, byte[] copyBuffer) throws IOException { 318 long bytesCopied = 0; 319 int read = -1; 320 321 while ((read = in.read(copyBuffer, 0, copyBuffer.length)) != -1) { 322 out.write(copyBuffer, 0, read); 323 bytesCopied += read; 324 } 325 return bytesCopied; 326 } 327 } 328 | Popular Tags |