Skip to content

Instantly share code, notes, and snippets.

@jbranchaud
Created February 7, 2012 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbranchaud/1761764 to your computer and use it in GitHub Desktop.
Save jbranchaud/1761764 to your computer and use it in GitHub Desktop.
Read file contents into a char[]
public char[] getFileContents(File file) {
// char array to store the file contents in
char[] contents = null;
try {
// Read in the contents line by line storing them in a StringBuffer
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
String line = "";
while((line = br.readLine()) != null) {
// append the content and the lost new line.
sb.append(line + "\n");
}
contents = new char[sb.length()];
sb.getChars(0, sb.length(), contents, 0);
assert(contents.length > 0);
}
catch(IOException e) {
System.out.println(e.getMessage());
}
return contents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment