I'm interested in simple line processing.
-
1In this newer, related question there's a good intro to JDK's built-in file IO classes: http://stackoverflow.com/questions/19430071/proper-java-classes-for-reading-and-writing-files-in-java – Jonik Dec 29 '13 at 12:32
7 Answers
Scanner:
for(Scanner sc = new Scanner(new File("my.file")); sc.hasNext(); ) {
String line = sc.nextLine();
... // do something with line
}

- 30,277
- 10
- 88
- 118
-
1Love the wide variety of methods, `Scanner` offers. Thanks. Simple and clean. – John Assymptoth Feb 05 '11 at 19:30
Take a look at the Scanner class.
It was added in Java 5 to make reading strings and files far easier, than the old FileReaders and FileInputStream chains (no more new BufferedReader(new FileReader())
just to get to a readLine
method).
In the Scanner class, you can use the nextLine
method to read a line at a time, but it also has lots of util methods for finding primitives and regular expressions in the file.

- 54,176
- 10
- 96
- 129
-
@Maxym: this answer needed to be complemented with an example of usage. Thanks. – John Assymptoth Feb 05 '11 at 19:33
You can use BufferedReader
, something like this:-
try {
BufferedReader input = new BufferedReader(new FileReader(new File("c:\\test.txt")));
try {
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} finally {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
-
1Scanner seems to be simpler and has more options. But thanks for putting the necessary code here. +1 – John Assymptoth Feb 05 '11 at 19:31
-
just "by the way", when Java7 comes, it will have "automated resource management" and it means that we won't need `try {...} finally { out.close() }` - closing will be done by JVM, and it will take care of it. – Maxym Feb 05 '11 at 19:37
If you're willing to make use of 3rd party libraries, then utility classes such as Files
from Guava or FileUtils
from Apache Commons IO make reading files very simple.
Examples below (where File file = new File("path/to/file.txt")
) of reading all lines of a text file into a List, and reading the whole file into a String.
Guava:
List<String> lines = Files.readLines(file, Charsets.UTF_8);
String contents = Files.toString(file, Charsets.UTF_8);
Apache Commons IO:
List<String> lines = FileUtils.readLines(file, "UTF-8");
String contents = FileUtils.readFileToString(file, "UTF-8")
My recommendation (as of 2013) is Guava, which is a modern, clean, actively maintained library. It's generally of higher quality than Apache Commons stuff.
Of course, adding Guava just for this wouldn't make sense, as it's a relatively big library. On the other hand, not using Guava in a Java project today would IMO be silly. :-)
Admittedly JDK now provides somewhat adequate tools (Scanner) for this particular purpose; using a 3rd party lib for reading files was more justified when something like this was the alternative.
-
I was trying not to use 3rd party libraries, indeed. But, I had heard of that library before. It seems to be gaining acceptance. – John Assymptoth Feb 05 '11 at 19:28
-
@John. Heh, "gaining acceptance" could be one wayof putting it. My experience is that libs from Apache Commons have been a mainstay in typical Java projects since early 00s (more recently joined/replaced by libs from Google, such as Guava). Of course, one should consider carefully before adding dependencies, but stuff like Commons IO will often pull its weight easily. – Jonik Feb 05 '11 at 19:42
You could try apache's FileUtils. methods like
for(String line: FileUtils.readLines(file)) {
System.out.println(line);
}

- 525,659
- 79
- 751
- 1,130
You can also use InputStreamReader (which you create using an InputStream like FileInputStream) wrapped in a BufferedReader for simple line reading of file.

- 2,158
- 2
- 21
- 23