gpt4 book ai didi

java - 将字符串转换为 RTF 格式

转载 作者:行者123 更新时间:2023-11-30 03:50:07 28 4
gpt4 key购买 nike

我想用我的程序生成 RTF 文档。我使用 RTFEditor,它以 rtf 格式提供编辑后的文本,但我也有 TextFields,我应该将 TextFields 中的字符串放入 RTF 文档中。我尝试编辑 rtf 文档文本,但我认为 ANSI 和 UTF-8 遇到了这个恼人的编码问题。如果我的 TextFields 得到带有“äöüß”的字符串......依此类推,我得到像“ö”这样的编码内容我的目标是获得一个美观的 RTF 文档,而没有编码问题。

处理此问题的最佳方法是什么?

这是我的类,它输出 rtf 文档:注意:请重点关注此代码中的重要内容。我希望我的代码中的德语术语不会让您感到困惑:)。

/**
* Handles the Saving of a Protokoll to a .rtf file.
* The .executeSaving() method executes the process of saving.
* @author me
*/
public class WordExport {

// other methods...

/**
* Executes the saving-progress by the given file.
* Structure of the rtf-File:
* -------------------------
* HeadString |
* -------------------------
* Content |
* .. |
* .. |
* .. |
* .. |
* -------------------------
* @param file points to the target where the .rtf should be saved.
* @param protokoll The protokoll which you want to save.
* @throws IOException Thrown when something is not okay with the IO.
*/
public void executeSaving(File file, Protokoll protokoll) throws IOException
{
//I want to insert my HeaderString into a specific position so i have to split etc...
String content = new String();
content = protokoll.getInhalt();
String[] split = content.split("}", 3);
for(int i = 0; i < split.length ; i++)
{
if(!split[i].contains("}"))
{
split[i] += "}";

}
}

String teilnehmer = new String();
for (Profil p : protokoll.getTeilnehmer())
{
teilnehmer += p.toString() + "\\par\n ";
}
String headString = new String();
//HERE IS THE IMPORTANT CODE --> here i put my strings bare into the rtf-format and i dont know how to handle the encoding.
headString = "\\f1\\fs44\\i0\\b0\\ul\\cf1\\ "+ protokoll.getTitel() +" \\par\n" +
"\\par\n" +
"\\fs24\\ul0 Raum: "+ protokoll.getRaum() +"\\par\n" +
"Zeitraum: "+ protokoll.getZeitraum() +"\\par\n" +
"Datum: "+ protokoll.getErstellungsdatum().toString() +"\\par\n" +
"\\par\n" +
"Teilnehmer: \\par\n" +
teilnehmer +
"\\ul0\\par ";

split[2] = headString+ split[2];

//Converts the String[] from above to the string, which you need to write into the .rtf file.
StringBuilder builder = new StringBuilder();
for(String s : split)
{
builder.append(s);
}
content = builder.toString();
if(!file.exists())
{
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
}
}

最佳答案

使用ISO-8859-1编码。此编码格式也将支持该字符。

关于java - 将字符串转换为 RTF 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24673598/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com