user1946492
user1946492

Reputation: 31

Does Scanner in Java block on file input?

I'm currently coding a log reader that should scan a log file that is currently being written to and then upload that data to a server. The file changes every hour. I was wondering if the next() method in the scanner blocks for end of file like it does for standard input. I couldn't find this information online in a google search.

Thank you.

Upvotes: 0

Views: 108

Answers (2)

DJClayworth
DJClayworth

Reputation: 26876

Technically correct answer: Yes, Scanner uses standard IO calls which may block on input. It will do this when data is unavailable but the end of the file has not been reached. This would be the case if it was reading from a socket.

The answer you are looking for: In the circumstances you are describing, scanner will not block because it has reached the end of the file. Even if the file might be written to in the future, EOF is generated when you try to read the end of the file.

Upvotes: 2

Sam Barnum
Sam Barnum

Reputation: 10724

This seems unlikely, as once it reaches the end of the file it assumes it is done, since there's no way for it to know that the file may change.

I'd also recommend looking at the source code for Scanner to double-check.

Upvotes: 0

Related Questions