将unicode写入rtf文件 [英] Writing unicode to rtf file

查看:135
本文介绍了将unicode写入rtf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用不同的语言将字符串写入rtf文件。我尝试了几件不同的事情。
我以日语为例,但对于我尝试过的其他语言来说,这是一样的。

I´m trying write strings in diffrent languages to a rtf file. I hav tried a few different things. I use japanese here as an example but it´s the same for other languages i have tried.

public void writeToFile(){

    String strJapanese = "日本語";
    DataOutputStream outStream;
    File file = new File("C:\\file.rtf");

    try{

        outStream = new DataOutputStream(new FileOutputStream(file));
        outStream.writeBytes(strJapanese);
        outStream.close();

    }catch (Exception e){
        System.out.println(e.toString());
    }
}

我已尝试过:

byte[] b = strJapanese.getBytes("UTF-8");
String output = new String(b);

或更具体:

byte[] b = strJapanese.getBytes("Shift-JIS");
String output = new String(b);

输出流还具有writeUTF方法:

The output stream also has the writeUTF method:

outStream.writeUTF(strJapanese);

可以使用write方法在输出流中直接使用byte []。以上所有这些都给了除了西欧语言以外的所有字符的乱码。要看看它是否工作,我已经尝试用notepad ++打开结果文档并设置了相应的编码。另外我使用OpenOffice,打开文档时您可以选择编码和字体。

You can use the byte[] directly in the output stream with the write method. All of the above gives me garbled characters for everything except west european languages. To see if it works I have tried opening the result document in notepad++ and set the appropriate encoding. Also i have used OpenOffice where you get to choose encoding and font when opening the document.

如果它的工作正常,但我的电脑无法正常打开,是否有

If it does work but my computer can´t open it properly, is there a way to check that?

推荐答案

默认情况下,JAVA中的sting是UTF-8(unicode),但是当你想写它需要指定编码

By default stings in JAVA are in UTF-8 (unicode), but when you want to write it down you need to specify encoding

try {
    FileOutputStream fos = new FileOutputStream("test.txt");
    Writer out = new OutputStreamWriter(fos, "UTF8");
    out.write(str);
    out.close();
} catch (IOException e) {
    e.printStackTrace();
}

ref: http://download.oracle.com/javase/tutorial/i18n/text/stream.html

这篇关于将unicode写入rtf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆