java.util.scanner - Java Scanner hasNextLine returns false -
i have several files (actually java source files saved in eclipse on ubuntu) need read , process line line. i've noticed cannot read 1 of files. code using below
try (scanner scanner = new scanner(file)) { while (scanner.hasnextline() ) { builder.append(scanner.nextline()).append("\n"); } } catch (filenotfoundexception ex) { system.out.println("error"); }
i checking beforehand if file exists. , does. can rename it. cannot read single line. hasnextline returns false. (i try hasnext).
at end take @ content of file , find there different looking character (which in comment section of java file). following character.
¸
when delete character, can read file normally. not acceptable. can read files character in it?
this character set issue, caused fact platform running java code uses default different set; always practice specify expected/needed character set used when parsing, , scanner class matter of calling constructor as:
scanner scanner = new scanner(file, "utf-8");
where second parameter character set literal, or better:
scanner scanner = new scanner(file, standardcharsets.utf_8);
Comments
Post a Comment