Skip to content

Instantly share code, notes, and snippets.

@arunreddy
Created June 9, 2012 13:20
Show Gist options
  • Save arunreddy/2900940 to your computer and use it in GitHub Desktop.
Save arunreddy/2900940 to your computer and use it in GitHub Desktop.
Read content from file.
private static String readFile() throws IOException {
FileInputStream stream = new FileInputStream(new File("String.txt"));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return Charset.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment