if you only want the whole text in one string than you can use the FileInputStream
FileInputStream inputStream = new FileInputStream("foo.txt");
try {
String everything = IOUtils.toString(inputStream);
} finally {
inputStream.close();
}
if you want to read the file line bye line use the BufferdReader
in Connection with StringBuilder
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
}finally {
br.close();
}
Note:
The examples are taken from @Knubo and the whole answer and further Information can be found
here