private String readResourceAsString ( Resource resource ) throws IOException { StringBuilder contents = new StringBuilder ( ) ; // Read entire resource into StringBuilder one chunk at a time Reader reader = new InputStreamReader ( resource . asStream ( ) , "UTF-8" ) ; try { char buffer [ ] = new char [ 8192 ] ; int length ; while ( ( length = reader . read ( buffer ) ) != - 1 ) { contents . append ( buffer , 0 , length ) ; } } // Ensure resource is always closed finally { reader . close ( ) ; } return contents . toString ( ) ; }
Reads the entire contents of the given resource as a String . The resource is assumed to be encoded in UTF - 8 .