简体   繁体   中英

Can't convert UTF-8 from Scanner to Console(System.out) with original result in Java

I know that java 8 uses UTF-8 as standard encoding. When i read string from standard-input(System.in) with Scanner using "UTF-8" then try to print it to console and the string is not in right origin as I show beneath ,but with the initialized string the console show right original string without unexpected result.How can i fix this?Please help me!
I use Scanner not String constructor to encode input.I know how to convert input to UTF-8 but something wrong happened to my result and asked for the reason and solution to solve that problem ,not asking for the way to encode string with UTF-8.

Here is my code:

import java.nio.charset.Charset;
import java.util.Scanner;


public class charset {

    public static void main(String[] args) {
        System.out.println(Charset.defaultCharset());
        Scanner sc =new Scanner(System.in,"UTF-8");
        System.out.println(sc.nextLine());
        System.out.println("á ă ấ");
    }
}

-Here is my output:

run:

UTF-8

á ă ấ

� ? ?

á ă ấ

BUILD SUCCESSFUL (total time: 10 seconds)

尝试使用这套utf8 eclipse如果它没有帮助,请尝试从文件而不是从控制台读取以查看会发生什么。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM