sockets - Java charset - How to get correct input from System.in? -
my first post here. well, i'm building simple app messaging through console(cmd , terminal), learning, i'm got problem while reader , writing text charset.
here initial code sending message, main.charset setted 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 receiving code:
try (bufferedreader br = new bufferedreader(new inputstreamreader(servidor,main.charset))){ string s; while ((s = br.readline()) != null) { system.out.println(s); } } when send "olá" or "ÁàçÇõÉ" (brazilian portuguese), got blank spaces on windows cmd (not tested in linux).
so teste following code:
scanner s = new scanner(system.in,main.charset); system.out.println(s.nextline()); and input "olá", printed "ol ".
the question is, how read console input read correctly , , can transmitted user , displayed correctly him.
if wanna output portuguese in text file, easy.
the thing have care display utf-8 encoding.
you can use simple way like
string text = "olá"; filewriter fw = new filewriter("hello.txt"); fw.write(text); fw.close(); then open hello.txt notepad or text tool support utf-8
or have change tool's default font utf-8.
if want show on console, think pvg answer you.
ok, seems still confuse on it.
here simple code can try.
scanner userinput = new scanner(system.in);//type olá plz string text = userinput.next(); system.out.println((int)text.charat(2));//you see output int 63 char word = 'á'; // word covert int 225 int = 225; system.out.println((int)word);// output 225 system.out.println((char)a); // output á so, conclusion?
if use console tpye in portuguese catch it, totally different word, not gibberish word.
Comments
Post a Comment