I've been utilizing the idiom underneath for quite a while. What's more, it is by all accounts the most far-reaching, in any sites on the locales I've visited.
Is there a superior/diverse approach to add a file to a string in Java?
private String readFile(String file) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader (file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("line.separator");
try {
while((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
return stringBuilder.toString();
} finally {
reader.close();
}
}