1 package org.igfay.util; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedReader ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.io.FileReader ; 10 import java.io.FileWriter ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.io.InputStreamReader ; 14 import java.io.ObjectInputStream ; 15 import java.io.ObjectOutputStream ; 16 import java.io.OutputStream ; 17 import java.io.PrintStream ; 18 import java.io.PrintWriter ; 19 import java.net.URL ; 20 import java.util.Iterator ; 21 22 import org.apache.log4j.Logger; 23 24 30 public class FileUtility { 31 32 protected static Logger log = Logger.getLogger(FileUtility.class); 33 34 39 public static String contentsOfFile(File file) { 40 String s = new String (); 41 char[] buff = new char[50000]; 42 InputStream is; 43 InputStreamReader reader; 44 try { 45 reader = new FileReader (file); 46 int nch; 47 while ((nch = reader.read(buff, 0, buff.length)) != -1) { 48 s = s + new String (buff, 0, nch); 49 } 50 } 51 catch (java.io.IOException ex) { 52 s = null; 53 } 54 return s; 55 } 56 57 58 64 public static String contentsOfFile(String fileName) { 65 return contentsOfFile(new File (fileName)); 66 } 67 68 71 public static void copy(File inFile, File outFile) throws IOException { 72 73 if (inFile.getCanonicalPath().equals(outFile.getCanonicalPath())) { 74 return; 77 } 78 FileInputStream fin = new FileInputStream (inFile); 79 FileOutputStream fout = new FileOutputStream (outFile); 80 copy(fin, fout); 81 } 82 83 86 public static void copy(InputStream in, OutputStream out) 87 throws IOException 88 { 89 90 synchronized (in) { 91 synchronized (out) { 92 93 byte[] buffer = new byte[256]; 94 try { 95 while (true) { 96 int bytesRead = in.read(buffer); 97 if (bytesRead == -1) 98 break; 99 out.write(buffer, 0, bytesRead); 100 } 101 } catch(IOException e) { 102 closeStreams(in,out); 103 throw e; 104 } 105 } 106 closeStreams(in,out); 107 108 } 109 110 } 111 112 118 public static void closeStreams(InputStream inputStream, OutputStream outputStream) { 119 try { 120 inputStream.close(); 121 outputStream.close(); 122 } catch(Exception e) { 123 } 124 } 125 126 127 134 public static boolean ensureFilePathExists(File file) { 135 File path; 136 try { 137 path = new File (file.getCanonicalPath()); 138 } 139 catch (IOException e) { 140 log.debug("IOException on file " + file); 141 return false; 142 } 143 if (!path.exists()) { 144 File parent = new File (path.getParent()); 145 log.debug("path does not exist, call self with parent " + parent); 146 ensurePathExists(parent); 147 } 148 return true; 149 } 150 151 152 158 public static boolean ensurePathExists(File file) { 159 File path; 160 try { 161 path = new File (file.getCanonicalPath()); 162 } 163 catch (IOException e) { 164 log.debug("IOException on file " + file); 165 return false; 166 } 167 if (!path.exists()) { 168 if (path.getParent() == null) { 169 log.debug("Operation terminating. Unable to get parent for "+path.getAbsolutePath() ); 170 return false; 171 } 172 File parent = new File (path.getParent()); 173 ensurePathExists(parent); 174 boolean result = path.mkdir(); 175 log.debug("result "+result+" mkdir for " + path); 176 return result; 177 178 } 179 return true; 180 } 181 182 183 191 public static boolean ensureFilePathExists(String fileString) { 192 return ensureFilePathExists(new File (fileString)); 193 } 194 195 196 201 public static boolean ensurePathExists(String fileString) { 202 return ensurePathExists(new File (fileString)); 203 } 204 205 206 public static PrintStream newPrintStreamOnFileNamed(File directory, String name) throws IOException { 207 File file = new File (directory, name); 208 log.debug("creating file " + name + " in " + directory.toString()); 209 return new PrintStream (new FileOutputStream (file)); 210 } 211 212 213 220 public static PrintWriter newPrintWriterOnFileNamed(File directory, String name) throws IOException { 221 File file = new File (directory, name); 222 return new PrintWriter (new FileOutputStream (file)); 223 } 224 225 226 235 public static Object readObjectFromBufferedFileObject(File file) throws IOException , ClassNotFoundException { 236 ObjectInputStream ois; 237 Object result = null; 238 log.debug("Opening input stream..."); 239 FileInputStream fis = new FileInputStream (file); 240 BufferedInputStream bis = new BufferedInputStream (fis, 4096); 241 ois = new ObjectInputStream (bis); 242 log.debug("Reading serialized object from stream..."); 243 result = ois.readObject(); 244 return (result); 245 } 246 247 248 256 public static Object readObjectFromFile(File file) throws IOException , ClassNotFoundException { 257 log.debug("File " + file.toString()); 258 return readObjectFromBufferedFileObject(file); 259 } 260 261 262 272 public static Object readObjectFromFile(File directory, String name) throws IOException , ClassNotFoundException { 273 File file; 274 file = new File (directory, name); 275 log.debug(directory.toString() + " " + name); 276 return readObjectFromBufferedFileObject(file); 277 } 278 279 280 289 public static Object readObjectFromFileWithPath(String fileWithPath) throws IOException , ClassNotFoundException { 290 return readObjectFromBufferedFileObject(new File (fileWithPath)); 291 } 292 293 294 297 public static void redirectStandardOutput() { 298 redirectStandardOutput(getLogFileName()); 299 } 300 301 302 306 public static void redirectStandardOutput(File file) { 307 try { 308 log.debug("redirectStandardOutput to " + file.getAbsolutePath()); 309 PrintStream stdout = new PrintStream (new FileOutputStream (file)); 310 System.setOut(stdout); 311 } 312 catch (Exception e) { 313 log.debug("Unable to redirect stdout to " + file.getAbsolutePath()); 314 } 315 } 316 317 318 322 public static void redirectStandardOutput(String fileName) { 323 redirectStandardOutput(new File (fileName)); 324 } 325 326 327 332 public static String stringFromFile(File file) { 333 return contentsOfFile(file); 334 } 335 336 337 342 public static String stringFromFile(String fileName) { 343 return contentsOfFile(new File (fileName)); 344 } 345 346 347 352 public static void stringToFile(String string, File file) { 353 354 try { 355 PrintWriter out = new PrintWriter (new FileWriter (file)); 356 out.print(string); 357 out.close(); 358 } 359 catch (FileNotFoundException fnfe) { 360 String msg = "File not found exception: " + file; 361 } 362 catch (IOException ioe) { 363 String msg = "IO Exception writing object output stream to file: " + file; 364 } 365 } 366 367 368 373 public static void stringToFile(String string, String fileString) { 374 375 stringToFile(string, new File (fileString)); 376 } 377 378 379 385 public static void stringToFileOutputStream(String string, FileOutputStream fos) throws IOException { 386 log.debug(""); 387 fos.write(string.getBytes()); 388 } 389 390 391 398 public static void writeObjectToFile(Object object, File directory, String name) throws IOException { 399 ObjectOutputStream oos; 400 File file; 401 402 log.debug("creating file " + name + " in " + directory.toString()); 403 file = new File (directory, name); 404 writeObjectToFileObject(object, file); 405 } 406 407 408 414 public static void writeObjectToFileObject(Object object, File file) throws IOException { 415 ObjectOutputStream oos = null; 416 log.debug("creating output stream on file..."); 417 oos = new ObjectOutputStream (new FileOutputStream (file)); 418 log.debug("writing object to stream..."); 419 oos.writeObject(object); 420 log.debug("closing stream..."); 421 oos.close(); 422 } 423 424 425 431 public static void writeObjectToFileWithPath(Object object, String fileAndPath) throws IOException { 432 writeObjectToFileObject(object, new File (fileAndPath)); 433 } 434 435 436 442 public static void iteratorToFile(Iterator it, String fileName) throws IOException { 443 log.debug(""); 444 FileOutputStream stream = new FileOutputStream (fileName); 445 while (it.hasNext()) { 446 stringToFileOutputStream(it.next() + "\n", stream); 447 } 448 449 } 450 451 452 458 public static java.io.BufferedReader getBufferedReaderFromFile(File file) throws FileNotFoundException { 459 BufferedReader bufferedReader = null; 460 FileReader frdr = new FileReader (file); 461 bufferedReader = new BufferedReader (frdr); 462 return bufferedReader; 463 } 464 465 466 472 public static java.io.BufferedReader getBufferedReaderFromFile(String fileString) throws FileNotFoundException { 473 return getBufferedReaderFromFile(new File (fileString)); 474 } 475 476 477 public static String getExceptionMessage(Exception e, File file) { 478 return file.toString() + ": " + e.toString() + " Msg: " + e.getMessage(); 479 } 480 481 485 public static String getLogFileName() { 486 487 java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat ("MMddyy.HHmmssSSS"); 489 String fileName = null; 491 return fileName; 492 } 493 494 500 public static File findExistingFile(String fileName) { 501 File file = null; 502 file = new File (fileName); 503 if (!file.exists()) { 504 file = null; 505 log.debug("find as resource"); 506 URL url = FileUtility.class.getResource("/" + fileName); 507 if (url != null) { 508 String urlFileName = url.getFile(); 509 file = new File (urlFileName); 510 } 511 } 512 513 if (file != null) { 514 log.debug("file "+file.getAbsolutePath()+" "+file.exists()); 515 } else { 516 log.debug("file null"); 517 } 518 return file; 519 } 520 521 526 public static String streamToString(InputStream inputStream) { 527 String s = new String (); 528 char[] buff = new char[50000]; 529 try { 530 InputStreamReader reader = new InputStreamReader (inputStream); 531 int nch; 532 while ((nch = reader.read(buff, 0, buff.length)) != -1) { 533 s = s + new String (buff, 0, nch); 534 } 535 } 536 catch (java.io.IOException ex) { 537 s = null; 538 } 539 return s; 540 } 541 542 } 543 | Popular Tags |