Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a file using Java scanner

One of the lines in a java file I'm trying to understand is as below.

return new Scanner(file).useDelimiter("\\Z").next();

The file is expected to return upto "The end of the input but for the final terminator, if any" as per java.util.regex.Pattern documentation. But what happens is it returns only the first 1024 characters from the file. Is this a limitation imposed by the regex Pattern matcher? Can this be overcome? Currently I'm going ahead using a filereader. But I would like to know the reason for this behaviour.

like image 212
Sharmila Avatar asked Oct 04 '10 17:10

Sharmila


People also ask

Can we read file using Scanner in Java?

We can also use both BufferReader and Scanner to read a text file line by line in Java.

How do I read an existing file in Java?

Pass a String (or File) with the relative path to your project folder (if you have your file inside src folder, this should be "src/Test. txt", not "Test. txt"). For read a text file you should use FileReader and BufferedReader, BufferedReader have methods for read completed lines, you can read until you found null.


2 Answers

Myself, I couldn't reproduce this. But I think I can shed light as to what is going on.

Internally, the Scanner uses a character buffer of 1024 characters. The Scanner will read from your Readable 1024 characters by default, if possible, and then apply the pattern.

The problem is in your pattern...it will always match the end of the input, but that doesn't mean the end of your input stream/data. When Java applies your pattern to the buffered data, it tries to find the first occurrence of the end of input. Since 1024 characters are in the buffer, the matching engine calls position 1024 the first match of the delimiter and everything before it is returned as the first token.

I don't think the end-of-input anchor is valid for use in the Scanner for that reason. It could be reading from an infinite stream, after all.

like image 70
Mark Peters Avatar answered Oct 29 '22 23:10

Mark Peters


Try wrapping the file object in a FileInputStream

like image 40
Amir Afghani Avatar answered Oct 29 '22 23:10

Amir Afghani