【问题标题】:Different outcome when writing to wordpad than writing to notepad using same code写入写字板与使用相同代码写入记事本时的结果不同
【发布时间】:2015-09-08 13:28:18
【问题描述】:

我目前正在为一款名为 D&D(龙与地下城)的纸笔 rp 游戏开发遭遇生成器。我有一个用记事本文件编写的怪物档案。当用户完成使用他想要的过滤器并按下“生成”程序过滤怪物时,读取带有给定怪物的记事本文件并将其写入一个新的记事本文件,然后打开该文件(使用可运行的执行程序)。当我从记事本到记事本阅读和写作时,这一切都按预期工作。然后我从我的测试人员那里收到了一些反馈,他们想要一些图片以及不同的文本格式。因此,我将存档文件更改为 RTF(word pad/富文本文件),并将完成的文件更改为相同的格式。我现在的问题是,即使它应该写入两个或更多,程序也只将一个怪物写入文件。

这里是reader和writer方法的代码

public void readerAndWriter()throws IOException
{
    destination = "Encounter.rtf";
    File EncounterFile = new File(source);
    FileInputStream fis =new FileInputStream(EncounterFile);
    BufferedReader in = new BufferedReader(new InputStreamReader(fis));

    FileWriter writer = new FileWriter(destination,true);
    BufferedWriter out = new BufferedWriter(writer);

    String aLine = null;
    while((aLine = in.readLine())!= null)
    {
        out.write(aLine);
        out.newLine();
    }
    in.close();
    out.close();
}

这里是使用读写器方法的方法的代码sn-p

if (monsters.get(t).getRace() == monsters.get(u).getRace())
{
             if (monsters.get(t).getCr() + monsters.get(u).getCr() ==  
                 getChosenCr())
                {

                      readAndWrite.setSource(monsters.get(t).getFilePath());
                      readAndWrite.readerAndWriter();
                      readAndWrite.setSource(monsters.get(u).getFilePath());
                      readAndWrite.readerAndWriter();

                        correctMonster = true;
                 }
else
   etc etc

感谢所有提示和提示。

【问题讨论】:

  • 你写的RTF格式有效吗?
  • @Kayaman 是的,使用可运行 exec 打开的文件会打开一个完美编写的写字板文件(读取的写字板文件中的所有信息都按预期存在),但它只写入第一个怪物(即使它应该写,说 2)
  • 你是在写完第一个怪物后立即打开文件吗?
  • @aksappy 不,文件是在写入所有怪物的方法运行完成后打开的。

标签: java wordpad


【解决方案1】:

如果我正确理解您的代码,您只需将几个文件的内容(您的怪物文件)相互附加。你可以用文本文件做到这一点,但 RTF 并不是那么简单(我猜你只会看到第一个怪物,因为 RTF 忽略了以下明显附加到 RTF 内容的 RTF 文档)。相反,你必须

  1. 为您的目标文件创建一个Document 实例。
  2. 将源文件中的所需内容读入javax.swing.text.Documents。
  3. 使用适当的 API 将源文档中的内容插入到目标文档中。
  4. 将目标文档写入新文件。

正如 ControlAltDel 评论的那样,您可以为此使用 RTFEditorKit(示例代码略微改编自 http://www.programcreek.com/java-api-examples/index.php?api=javax.swing.text.rtf.RTFEditorKit 的示例 5):

/**
 * Reads the file specified by path and writes its text content to out.
 * @param out Writer to output the text content
 * @param path name of an RTF file to read from
 */
public void simpleRtfExample(Writer out, String path) throws IOException {
    FileInputStream in = null;
    try {
        in = new FileInputStream(path);
        byte[] buffer = new byte[in.available()];
        in.read(buffer, 0, in.available());
        String input = new String(buffer);
        String result = null;
        try {
            RTFEditorKit rtfEditor = new RTFEditorKit();
            Document doc = rtfEditor.createDefaultDocument();

            // read the source RTF into doc:
            rtfEditor.read(new StringReader(input), doc, 0);

            // Get the text of the document as String.
            // Here you could use doc's API to access
            // its content in a more sophisticated manner.
            result = doc.getText(0,doc.getLength());
        } catch (Exception e) {
            e.printStackTrace();
        }
        out.write(result);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多