-1

我正在尝试获取一个以 Cp1252 编码编码并保存的字符串,并将其显示在 java 文本区域中。当我读回来时,它有带有问号的黑色菱形,其中特殊字符通常是('、& 等)。我应该怎么做才能格式化它以显示正确的字符。

我无法复制和粘贴文本,因为它在移出单词时几乎可以正确显示。但我用来读取 Cp1252 文件的代码如下:

try {
        br = new BufferedReader(new FileReader(f));
        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
            emi.stringContent += "\n" + strLine;
        }

    br.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(EMailTmpDirRead.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(EMailTmpDirRead.class.getName()).log(Level.SEVERE, null, ex);
    }

谢谢!

进行了以下编辑,现在没有任何内容读入。

StringBuffer temp2 = new StringBuffer(1000);
    int numRead = 0;
    char[] buf = new char[1024];

    try {
        ir = new InputStreamReader(new FileInputStream(f), "Cp1252");
        while((numRead = ir.read()) != -1)
        {
            String temp = String.valueOf(buf, 0, numRead);
            temp2.append(temp);
            buf = new char[1024];
        }
        emi.stringContent = temp2.toString();

行似乎被跳过 StringBuffer temp2 = new StringBuffer();

    try {
        ir = new InputStreamReader(new FileInputStream(f), "Cp1252");
        br = new BufferedReader(ir);
        while(br.readLine() != null)
        {
            temp2.append(br.readLine());
        }
        emi.stringContent = temp2.toString();
4

1 回答 1

0

不要使用 a FileReader,而是使用带有;的FileInputStream包裹 InputStreamReader在 InputStreamReader 的构造函数中指定字符编码(根据此页面,您应该使用“Cp1252”进行编码)

于 2013-05-10T03:23:45.557 回答