1 5 package SOFA.Util; 6 7 import java.io.*; 8 9 13 public class Tools { 14 19 public static void deleteSubTree (File file) { 20 if (file.isDirectory()) { 21 File[] subFiles = file.listFiles(); 22 for (int i = 0; i < subFiles.length; i++) { 23 File subFile = subFiles[i]; 24 Tools.deleteSubTree(subFile); } 26 } 27 file.delete(); 28 } 29 30 35 36 public static void deleteEmptyDirectoryTree(File file, File stopDir) 37 { 38 while (file != null && file.isDirectory() && !file.equals(stopDir) && file.list().length == 0) 39 { 40 file.delete(); 41 file = file.getParentFile(); 42 } 43 } 44 45 46 53 public static void moveFile (File srcFile, File destFile) throws IOException 54 { 55 destFile.getParentFile().mkdirs(); 56 if (srcFile.renameTo(destFile) == false) { FileInputStream src = new FileInputStream(srcFile); 59 FileOutputStream dest = new FileOutputStream(destFile); 60 61 copyStream(src, dest); 62 63 src.close(); 64 dest.close(); 65 destFile.setLastModified(srcFile.lastModified()); srcFile.delete(); 67 } 68 } 69 70 73 private static final int BUFFER_SIZE = 4096; 74 75 78 private static final byte[] buffer = new byte[BUFFER_SIZE]; 79 80 88 public static void copyStream (InputStream src, OutputStream dest) throws IOException { 89 int length; 90 for (; ;) { 91 synchronized (buffer) { 92 length = src.read(buffer); 93 if (length > 0) 94 dest.write(buffer, 0, length); 95 else 96 break; 97 } 98 } 99 } 100 101 } 102 | Popular Tags |