Scanner only reads file name and nothing else

You’ve misunderstood the API for Scanner. From the docs for the Scanner(String) constructor:

Constructs a new Scanner that produces values scanned from the specified string.

source – A string to scan

It’s not a filename – it’s just a string.

You should use the Scanner(File) constructor instead – or better yet, the Scanner(File, String) constructor to specify the encoding as well. For example:

try (Scanner scanner = new Scanner(new File(this.fileName), "UTF_8")) {

(Note the use of a try-with-resources statement so the scanner gets closed automatically.)

Leave a Comment