简体   繁体   English

从OS X到Windows的Java编码

[英]Java encoding from OS X to Windows

I have made a primitive multi-client chat with swing GUI. 我已经使用swing GUI进行了原始的多客户端聊天。 Everything works fine as long as both people write from the same OS. 只要两个人都在同一操作系统上编写,一切都可以正常工作。 If one of them writes from Windows and the other from OS X, the encoding of some special characters goes nuts. 如果其中一个是从Windows编写的,另一个是从OS X编写的,则某些特殊字符的编码将变得毫无意义。 ( I am from CZE, we use characters as š,ě,č,ř,ž...). (我来自CZE,我们使用š,ě,č,ř,ž...等字符)。 I have searched for a long time but didn't find anything that would help. 我已经搜索了很长时间,但没有找到任何有用的信息。

I have input and output defined as: 我的输入和输出定义为:

in = new BufferedReader(new InputStreamReader(soc.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream()));

where soc is the socket used for connecting to the server side. 其中soc是用于连接到服务器端的套接字。

The sending process is as simple as: 发送过程非常简单:

out.println(message);

where message is a String, which I got from JTextArea by calling method .getText() 其中message是一个字符串,我通过调用方法.getText()从JTextArea获得

I know why this problem occurs, but I was unable to find any reasonable solution. 我知道为什么会发生此问题,但是我找不到任何合理的解决方案。 Any help will be appreciated. 任何帮助将不胜感激。

Thanks 谢谢

When reading character data from Input/OutputStreams, it's a good practice to always specify the character encoding. 从Input / OutputStreams读取字符数据时,最好始终指定字符编码。 Otherwise the platform default encoding is used (which might not be the same on all systems). 否则,将使用平台默认编码(在所有系统上可能都不相同)。

in = new BufferedReader(new InputStreamReader(soc.getInputStream(), StandardCharsets.UTF_8));
out = new PrintWriter(new OutputStreamWriter(soc.getOutputStream(), StandardCharsets.UTF_8));

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

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