Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Scanner hasNextLine returns false

I have several files (actually they are also java source files saved in Eclipse on Ubuntu) which I need to read and process line by line. I've noticed that I cannot read one of the files. The code I am using is as below

try (Scanner scanner = new Scanner(file)) {
    while (scanner.hasNextLine() ) {
        builder.append(scanner.nextLine()).append("\n");
    }
} catch (FileNotFoundException ex) {
    System.out.println("Error");
}

I was checking beforehand if the file exists. And it does. I can even rename it. But I cannot read a single line. hasNextLine simply returns false. (I even try hasNext).

At the end I take a look at the content of the file and find that there is a different looking character (which was in the comment section of java file). It is the following character.

¸

When I delete this character, I can read the file normally. However this is not acceptable. What can I do to read the files even with that character in it?

like image 216
Nuri Tasdemir Avatar asked Jun 01 '15 00:06

Nuri Tasdemir


1 Answers

This is most probably a character set issue, caused by the fact that the platform you are running your java code uses by default a different set; it is always a good practice to specify the expected/needed character set to be used when parsing, and with the Scanner class is just a matter of calling the constructor as:

Scanner scanner = new Scanner(file, "UTF-8");

where the second parameter is the character set literal, or even better:

Scanner scanner = new Scanner(file, StandardCharsets.UTF_8);
like image 119
guido Avatar answered Sep 24 '22 07:09

guido