문제

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?

올바른 솔루션이 없습니다

다른 팁

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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top