1 16 17 package com.buchuki.util; 18 19 import java.io.*; 20 21 27 public class FileUtils { 28 29 32 private FileUtils() { 33 } 34 35 40 public static void deleteDirectory(File directory) throws IOException { 41 if (directory.isDirectory()) { 42 File[] files = directory.listFiles(); 43 for (File file : files) { 44 if (file.isDirectory()) { 45 deleteDirectory(file); 46 } else { 47 file.delete(); 48 } 49 } 50 } 51 directory.delete(); 52 } 53 54 60 public static void copyDirectory(File fromDirectory, File toDirectory) 61 throws IOException { 62 if (toDirectory.exists()) { 63 throw new IllegalArgumentException ( 64 "toDirectory " + toDirectory + "already exists"); 65 } 66 toDirectory.mkdirs(); 67 File[] files = fromDirectory.listFiles(); 68 for (File file : files) { 69 File newFile = new File(toDirectory, file.getName()); 70 if (file.isDirectory()) { 71 copyDirectory(file, newFile); 72 } else { 73 copyFile(file, newFile); 74 } 75 } 76 } 77 78 84 public static void copyFile(File from, File to) throws IOException { 85 if (to.exists()) { 86 throw new IllegalArgumentException ( 87 "toFile " + to + " exists"); 88 } 89 InputStream in = new BufferedInputStream(new FileInputStream(from)); 90 OutputStream out = new BufferedOutputStream(new FileOutputStream(to)); 91 92 int b; 93 while ((b = in.read()) != -1) { 94 out.write(b); 95 } 96 in.close(); 97 out.close(); 98 } 99 } 100 | Popular Tags |