Java 从System.in stream读取和写入UTF-8字符
如果我使用Java 从System.in stream读取和写入UTF-8字符,java,input,unicode,utf-8,inputstream,Java,Input,Unicode,Utf 8,Inputstream,如果我使用System.outstream的print方法在控制台上打印类似ελλληνικά的unicode字符串,它将按预期打印(因为我在支持UTF字符的输出控制台中使用Ubuntu mono) 但是,如果我尝试使用System.in stream从控制台读取UTF-8编码的unicode字符,则无法正确读取。 我尝试了许多不同的方法来实现它,使用System.in stream中的各种reader类,但它从来都不起作用。有人知道我能做什么吗 下面是一个代码示例 BufferedReader
System.out
stream的print
方法在控制台上打印类似ελλληνικά的unicode字符串,它将按预期打印(因为我在支持UTF字符的输出控制台中使用Ubuntu mono)
但是,如果我尝试使用System.in stream从控制台读取UTF-8编码的unicode字符,则无法正确读取。
我尝试了许多不同的方法来实现它,使用System.in stream中的各种reader类,但它从来都不起作用。有人知道我能做什么吗
下面是一个代码示例
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
系统。in
是一个输入流,它是一个字节流。您需要一个读取器
来读取字符。读者将为您解码
在这种情况下,您可以使用InputStreamReader
将System.In包装成,并将“UTF-8”作为第二个构造函数参数传递
Scanner console = new Scanner(new InputStreamReader(System.in, "UTF-8"));
while (console.hasNextLine())
System.out.println(console.nextLine());
更新:
很可能您的标准输入码的编码是错误的。要进行验证,可以将从
系统中获取的字节数组与预期的字节数组进行比较
byte [] expected = "Δέν".getBytes("UTF-8"); // [-50, -108, -50, -83, -50, -67]
byte [] fromStdin = new byte[1024];
int c = System.in.read(fromStdin);
for (int i = 0; i < c-1; i++) {
if (expected[i] != fromStdin[i]) {
System.out.println(i + ", " + fromStdin[i]);
}
}
它会读得乱七八糟。尝试使用java.io.Console.readLine()
或java.io.Console.readLine(字符串、对象…
)Console
实例由System.Console()
方法返回。例如:
package package01;
import java.io.Console;
public class Example {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.err.println("No console");
System.exit(1);
}
String s = console.readLine("Enter string: ");
System.out.println(s);
}
}
您可以发布您的无效代码吗?可能我怀疑您的系统的编码。in
是UTF-8
。我如何检查System.in的编码?无论如何,系统的属性“file.encoding”是UTF-8请参阅更新的答案。是的,您是对的。但是如何设置System.in的编码呢?但是为什么java.nio.charset.charset.defaultCharset()返回“UTF-8”?这不是System.in使用的编码吗?再次更新了答案。因此,我必须设置windows环境编码?我该怎么做?您可能想尝试使用控制台可能是正确的方法。但是,您应该知道,在IDE中。最好使用读取器
参数创建一个单独的读取方法。然后您可以使用System.console().reader()
调用它,或者尝试基于System.in
创建一个读卡器,甚至使用模拟读卡器进行单元测试。
package package01;
import java.io.Console;
public class Example {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.err.println("No console");
System.exit(1);
}
String s = console.readLine("Enter string: ");
System.out.println(s);
}
}