public static String stringFromFile ( String filename , String encoding ) { try { StringBuilder sb = new StringBuilder ( ) ; BufferedReader in = new BufferedReader ( new EncodingFileReader ( filename , encoding ) ) ; String line ; while ( ( line = in . readLine ( ) ) != null ) { sb . append ( line ) ; sb . append ( eolChar ) ; } in . close ( ) ; return sb . toString ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; return null ; } }
Returns the contents of a file as a single string . The string may be empty if the file is empty . If there is an IOException it is caught and null is returned . Encoding can also be specified .