Encoding OutputStream写入(int b)方法:特殊字符编码

Encoding OutputStream写入(int b)方法:特殊字符编码,encoding,char,jtextarea,outputstream,Encoding,Char,Jtextarea,Outputstream,我无法从OutputStream类中使用方法write(intb)来解决编写特殊字符的问题。我使用这个类将控制台输出重定向到JTextArea class CustomOutputStream extends OutputStream { private JTextArea textArea; CustomOutputStream(JTextArea textArea) { this.textArea = textArea; } @Overri

我无法从
OutputStream
类中使用方法
write(intb)
来解决编写特殊字符的问题。我使用这个类将控制台输出重定向到
JTextArea

class CustomOutputStream extends OutputStream {
    private JTextArea textArea;

    CustomOutputStream(JTextArea textArea) {
        this.textArea = textArea;
    }

    @Override
    public void write(int b) {
        textArea.append(String.valueOf((char) b));
        // scrolls the text area to the end of data
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }
}
它适用于标准字符。对于字符
它打印不可读的字符

我不知道如何设置字符编码或如何用其他方法解决这个问题


谢谢你,J.

你把
b
这是
int
类型转换成
char
类型,所以你丢失了大部分信息。而且您没有将
b
解释为unicode代码点

textArea.append(Character.toChars​(b))

如果您的原始文本不是Unicode,您可以对其进行解码。

您好,我尝试了您的解决方案,但在
java.lang.IllegalArgumentException
java.lang.Character.toChars
中得到了
textArea.append(String.valueOf(b));textArea.append(“-”)
?或者演示如何调用
write
?也许你们在调用时搞砸了编码,或者从一开始,它不是一个unicode字符串。我使用log4j2来记录控制台和文件-所有字符都很好。在我的GUI应用程序中,我通过
System.setOut(PrintStream)将控制台的输出重定向到
PrintStream
。所以,当有东西被记录到控制台时,会自动调用方法
write()。如果
textArea.append(String.valueOf(b)),则
;textArea.append(“.”)是
。ᅣ.ロ.ᅤ.ᄀ.ᅣ.ヘ.ᅤ.ル.ᅤ.ᄒ.ᅢ.ᄑ.ᅢ.ᄀ.ᅢ.ᆳ.ᅢ.ᄅ.日志似乎不再是Unicode字符(如果您在windows上:windows控制台通常使用非Unicode编码)。尝试以下操作:。UTF-16是预期的编码。