Adam Bien's Weblog
Reading A File Into A String - With A Single Line And JDK 1.6+
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerSample {
public static void main(String[] args) throws FileNotFoundException {
//Z means: "The end of the input but for the final terminator, if any"
String output = new Scanner(new File("file.txt")).useDelimiter("\\Z").next();
System.out.println("" + output);
}
}
[Thanks Paulo Silveira for the snippet!]
Posted at 11:47AM Sep 19, 2011 by Adam Bien in Real World Java EE Patterns - Rethinking Best Practices | Comments[10] | Views/Hits: 5593
*NEW* Workshop: "Effective Java EE 6/7", July 10th, Airport Munich Tweet Follow @AdamBien



Nice Java 6 hack. Scanner is so underestimated :-)
Posted by Marcus K. on September 19, 2011 at 12:04 PM CEST #
Scanner is generally great, but it's based on regular expression, so the performance could become bad in some situations. I remember I did a test to read a large text file line by line, and it turned out BufferedReader.readLine() is much more effective than Scanner.nextLine().
Posted by shinzey on September 19, 2011 at 01:21 PM CEST #
Adam, you have a typo in that code snippet.
It should be new File("file.txt") instead of new File("file.txt)
(see the missing ").
Posted by Maciej Biłas on September 19, 2011 at 01:48 PM CEST #
@Maciej,
thanks! Corrected that. In my working example looked like:
File file = new File("./file.txt");
String output = new Scanner(file).useDelimiter("\\Z").next();
but I shortened it for the post. This is the reason for the typo.
Thanks!,
adam
Posted by Adam Bien on September 19, 2011 at 09:59 PM CEST #
@Shinzey
Premature Optimization Is The Root Of All Over-Engineering :-). But you are right. The compilation of regexes is noticeable. I try always to measure the performance before I optimize it.
With NetBeans it is just another button to hit :-)
thanks for your comment!,
adam
Posted by Adam Bien on September 19, 2011 at 10:02 PM CEST #
This idiom is used in the German book "Java ist auch eine Insel" for several years. But it has a mayor problem: the underlying ReadableByteChannel is not closed and this is a resource leak (same with your Files.newBufferedReader() examples, you don't close them). Starting with Java 7 try-with-resources is nice helper because Scanner is Closeable and thus AutoCloseable.
Posted by Christian on September 19, 2011 at 10:08 PM CEST #
Thank you for this interesting post Adam!
Posted by 88.149.245.206 on September 20, 2011 at 01:00 AM CEST #
Thanks for introducing me to Scanner. I had not seen it before and we can use it in our current project.
It is also present in Java 5.0.
Posted by Michael Strasser on September 20, 2011 at 09:37 AM CEST #
Thanks for the credits. As Michael Strasser pointed out, it is valid since Java 5.
Although pratical, we need to be careful: if scanning a InputStream like System.in or a socket one, it will only return from the .next() invocation when EOF is found.
Posted by Paulo Silveira on September 20, 2011 at 04:48 PM CEST #
Hi Adam,
thanks for pointing out Scanner, I never stumpled over that but used commons-io for such tasks.
But I think it would be worth mentioning that Scanner support charsets as well when reading from files or input streams because lots of people tend to forget that and rely on system charset.
See you on W-JAX,
Stephan
Posted by Stephan on October 30, 2011 at 10:42 PM CET #