1 6 7 package org.contineo.core; 8 9 import java.io.BufferedInputStream ; 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileOutputStream ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.OutputStream ; 16 17 import org.apache.log4j.Level; 18 import org.apache.log4j.Logger; 19 24 public class FileBean { 25 static final int BUFF_SIZE = 100000; 26 static final byte[] buffer = new byte[BUFF_SIZE]; 27 28 33 public static boolean deleteFile(String filename) { 34 File f = new File (filename); 35 boolean result = false; 36 try { 37 result = f.delete(); 38 } 39 catch (Exception ex) { 40 logError(ex.getMessage()); 41 } 42 return result; 43 } 44 45 public static boolean createDir(String dirname) { 46 File f = new File (dirname); 47 boolean result = false; 48 try { 49 result = f.mkdirs(); 50 } 51 catch (Exception ex) { 52 logError(ex.getMessage()); 53 } 54 return result; 55 } 56 57 public static boolean renameFile(String filename, String newfilename) { 58 File f = new File (filename); 59 File nf = new File (newfilename); 60 boolean result = false; 61 try { 62 result = f.renameTo(nf); 63 } 64 catch (Exception ex) { 65 logError(ex.getMessage()); 66 } 67 return result; 68 } 69 70 public static boolean copyFile(String filename, String newfilename) { 71 boolean result = false; 72 try { 73 File out = new File (newfilename); 74 InputStream fis = new FileInputStream (filename); 75 OutputStream fos = new FileOutputStream (out); 76 int buffer = 0; 77 while ((buffer = fis.read()) != -1) { 78 fos.write(buffer); 79 } 80 fis.close(); 81 fos.close(); 82 result = true; 83 } 84 catch (Exception ex) { 85 logError(ex.getMessage()); 86 } 87 return result; 88 } 89 90 public static boolean copyDir(String dirname, String newdir) { 91 boolean result = false; 92 try { 93 createDir(newdir); 94 File f = new File (dirname); 95 if (f.isDirectory()) { 96 String [] files = f.list(); 97 for (int i=0; i<files.length; i++) { 98 File file = new File (dirname + "/" + files[i]); 99 if (file.isDirectory()) 100 copyDir(f.getAbsolutePath() + "/" + files[i], newdir + "/" + files[i]); 101 else 102 copyFile(f.getAbsolutePath() + "/" + files[i], newdir + "/" + files[i]); 103 } 104 } else { 105 copyFile(dirname, newdir); 106 } 107 result = true; 108 } catch (Exception e) { 109 logError(e.getMessage()); 110 } 111 return result; 112 } 113 114 public static boolean deleteDir(String dirname) { 115 try { 116 return deleteDir(new File (dirname)); 117 } catch (Exception ex) { 118 logError(ex.getMessage()); 119 } 120 return false; 121 } 122 123 public static boolean deleteDir(File file) { 124 boolean result = true; 125 try { 126 if (!file.exists()) 127 return true; 128 129 if (file.isDirectory()) { 130 File [] files = file.listFiles(); 131 for (int i=0;i<files.length;i++) { 132 result = deleteDir(files[i]); 133 } 134 result = file.delete(); 135 } else { 136 result = file.delete(); 137 } 138 } catch (Exception ex) { 139 logError(ex.getMessage()); 140 result = false; 141 } 142 return result; 143 } 144 145 public static boolean exists(String filename) { 146 try { 147 File file = new File (filename); 148 return file.exists(); 149 } catch (Exception e) { 150 logError(e.getMessage()); 151 return false; 152 } 153 } 154 155 public static long getSize(String filename) { 156 File f = new File (filename); 157 if (f.length() > 1024) 158 return f.length(); 159 else 160 return 1024; 161 } 162 163 public static StringBuffer readFile(String filename) { 164 StringBuffer content = new StringBuffer (); 165 try { 166 File file = new File (filename); 167 BufferedInputStream bis = new BufferedInputStream (new FileInputStream (file)); 168 int ichar = 0; 169 while ((ichar = bis.read()) > 0) { 170 content.append((char)ichar); 171 } 172 bis.close(); 173 } catch (Exception ex) { 174 logError(ex.getMessage()); 175 } 176 177 return content; 178 } 179 180 public static void writeFile(InputStream in, String filepath) { 181 OutputStream os = null; 182 try { 183 os = new FileOutputStream (filepath); 184 185 while (true) { 186 synchronized (buffer) { 187 int amountRead = in.read(buffer); 188 if (amountRead == -1) 189 break; 190 os.write(buffer, 0, amountRead); 191 } 192 } 193 } 194 catch (Exception e) { 195 logError(e.getMessage()); 196 } 197 finally { 198 try { 199 if (in != null) 200 in.close(); 201 if (os != null) 202 os.close(); 203 } catch (IOException e) { 204 logError(e.getMessage()); 205 } 206 } 207 } 208 209 public static void writeFile(String text, String filepath) { 210 try { 211 OutputStream bos = new FileOutputStream (filepath); 212 bos.write(text.getBytes()); 213 bos.close(); 214 } 215 catch (Exception e) { 216 logError(e.getMessage()); 217 } 218 } 219 220 public static void appendFile(String text, String filepath) { 221 try { 222 OutputStream bos = new FileOutputStream (filepath, true); 223 bos.write(text.getBytes()); 224 bos.close(); 225 } catch (Exception e) { 226 logError(e.getMessage()); 227 } 228 } 229 230 private static void logError(String message) { 231 Logger logger = LoggingManager.getLogger(org.contineo.core.FileBean.class); 232 if (logger.isEnabledFor(Level.ERROR)) 233 logger.error(message); 234 } 235 } 236 | Popular Tags |