【发布时间】:2017-04-06 20:40:56
【问题描述】:
如果我使用System.out 流的print 方法在控制台上打印像 ελληνικά 这样的 unicode 字符串,它会按预期打印(因为我在支持 UTF 字符的输出控制台中使用 Ubuntu mono )。
但是,如果我尝试使用 System.in 流从带有 UTF-8 编码的控制台 unicode 字符中读取,则无法正确读取。 我已经尝试了许多不同的方法来使用带有 System.in 流的各种阅读器类来实现它,但它从来没有工作过。那么有没有人知道我可以做到这一点的方法
这是一个代码示例
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
BufferedWriter console = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
console.write("p1: Γίνεται πάντως\n");
console.flush();
System.out.println("p2: Γίνεται πάντως");
byte dataBytes[] = keyboard.readLine().getBytes(Charset.forName("UTF-8"));
System.out.println("p3: " + new String(dataBytes));
console.write("p4: " + new String(dataBytes, "UTF-8") + "\n");
console.flush();
Scanner scan = new Scanner(System.in, "UTF-8");
System.out.println("p5: " + (char) System.in.read());
System.out.println("p6: " + scan.nextLine());
System.out.println("p7: " + keyboard.readLine());
我的控制台上的输出:
p1: Γίνεται πάντως
p2: Γίνεται πάντως
Δέν
p3: ���
p4: ���
Δέν
p5: Ä
p6: ��
Δέν
p7: ���
我的 IDE 是 Netbeans
【问题讨论】:
-
你能发布你不工作的代码吗?
-
我怀疑你的
System.in的编码是UTF-8。 -
如何检查 System.in 的编码?无论如何,系统的属性“file.encoding”是 UTF-8
-
查看更新的答案。
标签: java input unicode utf-8 inputstream