Java charset - How to read correctly from System.in?

Asked

Viewed 135 times

0

I am creating an application to exchange messages between computers using Sockets, through cmd or terminal (I want it to run on linux and windows, console application).

But I’m having trouble coding. When I read an entry through System.in, on the Windows 7 console, the output is bugged. With the following code:

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

Accented or special characters are not displayed correctly.

I’m using the following code to transmit messages:

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 to receive:

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

The question is, how to properly read from the console and stream so that the other user receives and is able to view on their console?

  • Are you using an IDE? I remember having trouble displaying special characters on the Netbeans console, I spent days looking for a solution and nothing. Until I discovered that the encoding shown on the console is not Utf-8 and so was bugged.

  • @Renan, I use Netbeans and depending on the charset that I use for Scanner, it does not display, but I am testing the application on the same Windows 7 console. I don’t know if cmd has its input encoding, and when we use Scanner in System.in with some charset, it just does something like: 'á in cp1252, byte = 160 -> tries to read the same byte = 160 in utf-8'

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.