1 18 19 package sync4j.syncclient.common; 20 21 import java.io.*; 22 import java.util.Vector ; 23 24 import sync4j.syncclient.common.VectorTools; 25 34 35 public class FileSystemTools { 36 37 39 44 public static void removeDirectoryTree(String directoryName) 45 throws Exception { 46 47 File directory = new File(directoryName); 48 49 if (!directory.exists()) { 50 return; 51 } 52 53 if (!directory.isDirectory()) { 54 throw new Exception ("'" + directory + "' is not a directory"); 55 } 56 57 String [] fileList = directory.list(); 58 int numFile = fileList.length; 59 boolean fileDeleted = false; 60 File f = null; 61 for (int i=0; i<numFile; i++) { 62 f = new File(directoryName + File.separator + fileList[i]); 63 if (f.isDirectory()) { 64 removeDirectoryTree(f.getPath()); 65 } else { 66 fileDeleted = f.delete(); 67 } 68 } 69 fileDeleted = directory.delete(); 70 } 71 72 83 public static void createFile(String directoryName, 84 String fileName, byte[] content) 85 throws Exception { 86 87 int size = content.length; 88 89 File fileOut = new File(directoryName + 90 File.separator + fileName); 91 92 fileOut.mkdirs(); 93 94 if (fileOut.exists()) { 95 fileOut.delete(); 96 } 97 98 FileOutputStream fout = new FileOutputStream(fileOut); 99 100 fout.write(content); 101 fout.close(); 102 103 } 104 105 106 117 public static void createFile(String directoryName, 118 String fileName, ByteArrayOutputStream byteStream) 119 throws Exception { 120 121 File fileOut = new File(directoryName + 122 File.separator + fileName); 123 124 fileOut.mkdirs(); 125 126 if (fileOut.exists()) { 127 fileOut.delete(); 128 } 129 130 FileOutputStream fout = new FileOutputStream(fileOut); 131 byteStream.writeTo(fout); 132 fout.close(); 133 134 } 135 136 147 public static void writeTextFile(File file, String text, boolean append) 148 throws IOException { 149 FileOutputStream fos = null; 150 151 try { 152 fos = new FileOutputStream(file.getAbsolutePath(), append); 153 154 if (text != null) { 155 fos.write(text.getBytes()); 156 } 157 158 } finally { 159 if (fos != null) { 160 fos.close(); 161 } 162 } 163 } 164 165 166 172 public static byte[] getFile(String fileName) throws IOException { 173 File file = new File(fileName); 174 int dim = (int)file.length(); 175 byte[] content = new byte[dim]; 176 177 BufferedInputStream stream = 178 new BufferedInputStream(new FileInputStream(fileName)); 179 stream.read(content); 180 stream.close(); 181 return content; 182 } 183 184 194 public static String [] getAllFiles(String directory, 195 String extension) { 196 197 ExtensionFilter filter = new ExtensionFilter(extension); 198 199 Vector fileList = new Vector (); 200 201 String [] files = new File(directory).list(filter); 202 203 if ((files == null) || (files.length == 0)) { 204 return new String [0]; 205 } 206 207 File f; 208 String [] children; 209 for (int i=0; i<files.length; ++i) { 210 f = new File(directory, files[i]); 211 if (f.isDirectory()) { 212 children = getAllFiles(f.getPath(), extension); 213 VectorTools.add(fileList, children); 214 } 215 if (f.isFile()){ 219 fileList.addElement(f.getPath()); 220 } 221 } 222 223 return VectorTools.toStringArray(fileList); 224 } 225 } | Popular Tags |