Created
June 14, 2012 14:16
-
-
Save danielpcox/2930613 to your computer and use it in GitHub Desktop.
A little Java snippet for reading a file into a string.
This file contains 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
private static String readFileAsString(String filePath) | |
throws java.io.IOException{ | |
StringBuffer fileData = new StringBuffer(1000); | |
BufferedReader reader = new BufferedReader( | |
new FileReader(filePath)); | |
char[] buf = new char[1024]; | |
int numRead=0; | |
while((numRead=reader.read(buf)) != -1){ | |
String readData = String.valueOf(buf, 0, numRead); | |
fileData.append(readData); | |
buf = new char[1024]; | |
} | |
reader.close(); | |
return fileData.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment