Created
February 7, 2012 20:30
-
-
Save jbranchaud/1761764 to your computer and use it in GitHub Desktop.
Read file contents into a char[]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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