简体   繁体   English

Java charset - 如何从System.in获得正确的输入?

[英]Java charset - How to get correct input from System.in?

my first post here. 我的第一篇文章。 Well, i'm building a simple app for messaging through console(cmd and terminal), just for learning, but i'm got a problem while reader and writing the text with a charset. 好吧,我正在构建一个简单的应用程序,通过控制台(cmd和终端)进行消息传递,只是为了学习,但我在阅读器和用charset写文本时遇到了问题。

Here is my initial code for sending message, the Main.CHARSET was setted to UTF-8: 这是我发送消息的初始代码,Main.CHARSET设置为UTF-8:

Scanner teclado = new Scanner(System.in,Main.CHARSET);
BufferedWriter saida = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(cliente.getOutputStream()),Main.CHARSET)));
saida.write(nick + " conectado!");
saida.flush();
while (teclado.hasNextLine()) {
    saida.write(nick +": "+ s);
    saida.flush();
}

And the receiving code: 和接收代码:

try (BufferedReader br = new BufferedReader(new InputStreamReader(servidor,Main.CHARSET))){
    String s;
    while ((s = br.readLine()) != null) {
        System.out.println(s);
    }
}

When i send "olá" or anything like "ÁàçÇõÉ" (Brazilian portuguese), i got just blank spaces on windows cmd (not tested in linux). 当我发送“olá”或类似“ÁàçÇõÉ”(巴西葡萄牙语)的东西时,我在Windows cmd上只有空白区域(未在linux中测试过)。

So i teste the following code: 所以我测试以下代码:

Scanner s = new Scanner(System.in,Main.CHARSET);
System.out.println(s.nextLine());

And for input "olá", printed "ol ". 输入“olá”,打印“ol”。

the question is, how to read the console so that the input is read correctly , and can be transmitted to another user and be displayed correctly to him. 问题是,如何读取控制台以便正确读取输入,并可以传输给另一个用户并正确显示给他。

if you just wanna output portuguese in text file, it would be easy. 如果你只想在文本文件中输出葡萄牙语,那将很容易。

The only thing you have to care about is display by UTF-8 encoding. 您唯一需要关心的是通过UTF-8编码显示。

you can use a really simple way like 你可以用一种非常简单的方式

    String text = "olá";
    FileWriter fw = new FileWriter("hello.txt");
    fw.write(text);
    fw.close();

Then open hello.txt by notepad or any text tool that support UTF-8 然后用记事本或任何支持UTF-8的文本工具打开hello.txt

or you have to change your tool's default font into UTF-8. 或者您必须将工具的默认字体更改为UTF-8。


If you want show it on console, I think pvg already answer you. 如果你想在控制台上显示它,我想pvg已经回答你了。


OK, seems you still get confuse on it. 好吧,似乎你仍然对此感到困惑。

here is a simple code you can try. 这是一个你可以尝试的简单代码。

        Scanner userInput = new Scanner(System.in);//type olá plz
        String text = userInput.next();
        System.out.println((int)text.charAt(2));//you will see output int is 63        

        char word = 'á'; // this word covert to int is 225
        int a = 225;
        System.out.println((int)word);// output 225
        System.out.println((char)a);  // output  á

So, what is the conclusion? 那么,结论是什么?

If you use console to tpye in portuguese then catch it, you totally get different word, not a gibberish word. 如果你用葡萄牙语使用控制台tpye然后抓住它,你完全得到不同的词,而不是一个胡言乱语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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