1 12 package org.openbravo.utils; 13 14 import java.io.*; 15 import org.apache.log4j.Logger; 16 17 21 public class FileUtility { 22 private String dir; 23 private String filename; 24 static Logger log4j = Logger.getLogger(FileUtility.class); 25 26 public FileUtility() { 27 } 28 29 public FileUtility(String path, String name) throws IOException { 30 this(path, name, true); 31 } 32 33 public FileUtility(String path, String name, boolean newFile) throws IOException { 34 if (path == null || path.equals("")) throw new IllegalArgumentException ("directory cannot be null"); 35 if (name == null || name.equals("")) throw new IllegalArgumentException ("file name cannot be null"); 36 37 File fpath = new File(path); 38 if(!fpath.isDirectory()) 39 throw new IllegalArgumentException ("Not a directory: " + path); 40 if(!fpath.canWrite()) 41 throw new IllegalArgumentException ("Not writable: " + path); 42 43 if (newFile) { 44 File file = new File(path, name); 45 if (file.canRead()) throw new IllegalArgumentException ("file: " + path + "\\" + name + " allready exists"); 46 } 47 48 dir = path; 49 filename = name; 50 } 51 52 public boolean ByteArrayToFile(ByteArrayOutputStream in) throws IOException { 53 if (in==null) return false; 54 File f = new File(dir, filename); 55 FileOutputStream fos = new FileOutputStream(f); 56 in.writeTo(fos); 57 fos.close(); 58 return true; 59 } 60 61 public boolean StringToFile(String in) throws IOException { 62 if (in==null) return false; 63 File f = new File(dir, filename); 64 FileWriter fileWriterData = new FileWriter(f); 65 PrintWriter printWriterData = new PrintWriter(fileWriterData); 66 printWriterData.print(in); 67 printWriterData.close(); 68 fileWriterData.close(); 69 return true; 70 } 71 72 public void dumpFile(OutputStream outputstream) { 73 byte abyte0[] = new byte[4096]; 74 try { 75 BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(new File(dir, filename))); 76 int i; 77 while((i = bufferedinputstream.read(abyte0, 0, 4096)) != -1) 78 outputstream.write(abyte0, 0, i); 79 bufferedinputstream.close(); 80 } 81 catch(Exception exception) { } 82 } 83 84 public boolean deleteFile() throws IOException { 85 File f = new File(dir, filename); 86 return f.delete(); 87 } 88 89 public int copy(File source, File destiny, DirFilter dirFilter, boolean discardHidden, boolean overwrite) throws Exception { 90 File[] list; 91 int total = 0; 92 if(dirFilter!=null) list = source.listFiles(dirFilter); 93 else list = source.listFiles(); 94 for(int i = 0; i < list.length; i++) { 95 File fileItem = list[i]; 96 if (fileItem.isDirectory()) { 97 if (!discardHidden || !fileItem.isHidden()) { 98 File faux = new File(destiny, fileItem.getName()); 99 faux.mkdir(); 100 total += copy(fileItem, faux, dirFilter, discardHidden, overwrite); 102 } 103 } else { 104 File fileAux = new File(destiny, fileItem.getName()); 105 if (overwrite || !fileAux.exists() || fileItem.lastModified()>fileAux.lastModified()) { 106 copyFile(fileItem, fileAux); 107 total++; 108 } 109 } 110 } 111 return total; 112 } 113 114 public void copyFile(File in, File out) throws Exception { 115 BufferedReader fileBuffer = new BufferedReader(new FileReader(in)); 117 FileOutputStream fos = new FileOutputStream(out); 118 OutputStreamWriter printWriterData = new OutputStreamWriter(fos, "UTF-8"); 119 String nextLine = fileBuffer.readLine(); 120 while (nextLine != null) { 121 printWriterData.write(nextLine); 122 printWriterData.write("\n"); 123 nextLine = fileBuffer.readLine(); 124 } 125 printWriterData.flush(); 126 fos.close(); 127 fileBuffer.close(); 128 133 } 134 135 public void delete(File source) throws Exception { 136 File[] list = source.listFiles(); 137 for(int i = 0; i < list.length; i++) { 138 File fileItem = list[i]; 139 if (fileItem.isDirectory()) { 140 delete(fileItem); 141 } 142 fileItem.delete(); 143 } 144 } 145 } 146 | Popular Tags |