Question

I'm trying to use Scanner class to parse text files. But it turns out that if the file contains Russian words the scanner can't read the file at all. scanner.hasNextLine() returns false at its very first call. Is this normal behavior of the Scanner class? Can I do something to fix the problem?

No correct solution

OTHER TIPS

To read text, containing another encoding, you should use the constructor of Scanner with additional parameter "encoding". For example, if file, containg russian symbols is in UTF-8 encoding, try something like this:

String path = ... // full path of file
Scanner sc = new Scanner(new FileInputStream(path), "UTF-8");

//read file line by line
while (sc.hasNextLine()){
    //read one line
    String s = sc.nextLine();
    //do something with line
    System.out.println(s);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top