Tackle verbosity and static import
Rémi Forax
forax at univ-mlv.fr
Thu Apr 14 10:43:36 PDT 2011
Playing a little bit with NIO file API, I come with this snippet for
printing
all lines of a file.
public static void main(String[] args) throws IOException {
for(String line: readAllLines(get("foo.txt"), defaultCharset())) {
System.out.println(line);
}
}
The trick is to abuse of static import, the whole file is:
import static java.nio.file.Files.*;
import static java.nio.file.Paths.*;
import static java.nio.charset.Charset.*;
import java.io.IOException;
public class LessVerbosity {
public static void main(String[] args) throws IOException {
for(String line: readAllLines(get("foo.txt"), defaultCharset())) {
System.out.println(line);
}
}
}
In my opinion, it shows that Paths.get should be renamed to Paths.getPath()
to be meaningful if used with static import.
Also, it should be cool to have an overload version of Files.readAllLines
that doesn't require a charset and use the default charset.
Rémi
More information about the nio-dev
mailing list