1 23 24 29 30 31 package com.sun.appserv.management.util.misc; 32 33 import java.io.File ; 34 import java.io.FileReader ; 35 import java.io.FileWriter ; 36 import java.io.IOException ; 37 import java.io.FileNotFoundException ; 38 39 42 public final class FileUtils 43 { 44 private FileUtils() {} 45 46 47 public static String 48 fileToString( final File src ) 49 throws FileNotFoundException , IOException 50 { 51 return fileToString( src, 32 * 1024 ); 52 } 53 54 public static String 55 fileToString( final File src, final int readBufferSize ) 56 throws FileNotFoundException , IOException 57 { 58 final FileReader in = new FileReader ( src ); 59 60 final long length = src.length(); 61 if ( length > 1024 * 1024 * 1024 ) 62 { 63 throw new IllegalArgumentException (); 64 } 65 66 final char[] readBuffer = new char[ readBufferSize ]; 67 68 final StringBuilder result = new StringBuilder ( (int)length ); 69 try 70 { 71 while ( true ) 72 { 73 final int numRead = in.read( readBuffer, 0, readBufferSize ); 74 if ( numRead < 0 ) 75 break; 76 77 result.append( readBuffer, 0, numRead ); 78 } 79 } 80 finally 81 { 82 in.close(); 83 } 84 85 return( result.toString() ); 86 } 87 88 public static void 89 stringToFile( final String s, final File dest ) 90 throws IOException 91 { 92 final FileWriter out = new FileWriter ( dest ); 93 94 try 95 { 96 out.write( s ); 97 } 98 finally 99 { 100 out.close(); 101 } 102 } 103 }; 104 105 106 | Popular Tags |