Frage

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?

Keine korrekte Lösung

Andere Tipps

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);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top