Reading A File Into A List - With JDK 1.7 …And Without Streams

import static java.nio.charset.Charset.*;
import static java.nio.file.Files.*;
import static java.nio.file.Paths.*;
import java.util.List;

public class FileReader {

    public static void read(String file) throws IOException{
        List<String> allLines = readAllLines(get(file), defaultCharset());
        for (String line : allLines) {

    public static void main(String[] args) throws IOException {

[Thanks to @Sander_Mak for the idea]


And this works only in JDK7? Why isn´t this possible in jdk6?

Posted by Fernando Cassia on August 09, 2011 at 01:36 PM CEST #

oh those static imports.... do not make this code readable, in my opinion

Posted by karl on August 09, 2011 at 05:00 PM CEST #

I do not like these static wildcard imports...which method belongs to which class?

Posted by Martin on August 10, 2011 at 01:05 AM CEST #

I think the list should be like this??
List <String> allLines = readAllLines(get(file), defaultCharset());

Posted by Gurnah on August 10, 2011 at 12:36 PM CEST #

How is this not a C like example?

Posted by Jeff on August 10, 2011 at 04:40 PM CEST #

Where's the implementation of readAllLines?

Posted by Sigal on August 11, 2011 at 06:14 PM CEST #

So when an 'Autocloseable' gets out of scope it gets closed?

how about for example jdbc-Connections from a Pool? looks error-prone to me...

Posted by Gustav on August 11, 2011 at 06:55 PM CEST #


you are right: it is List<String> -> I forgot to escape the generic brackets and it was swallowed by the browser,

thanks for the reminder!,


Posted by Adam Bien on August 13, 2011 at 02:54 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments