1

I am currently working on an encounter generator for a pen and paper rp game known as D&D(Dungeons and dragons). I have an archive of monsters written in notepad files. When the user is finished using the filters he wants and presses "generate" the program filters through the monsters, reads the notepad file with the given monster(s) and writes it(them) to a new notepad file which is then opened (using runnable exec). This all worked as intended when I read and wrote from notepad to notepad. Then I received some feedback from my testers that they would like some pictures as well as a different formating of the text. Therefore i changed the archive files to RTF (word pad / rich text file) and also changed the finished file to the same format. My problem now, is that the program only writes one monster to the file even if its supposed to write two or more.

Here is the code for the reader and writer method

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();
}

Here is the code snippet for the method that is using the reader and writer method

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

all tips and hints are appreciated.

1 Answers1

0

If I understand your code right, you just append the contents of several files to each other (your monster files). You can do that with text files, but RTF is not that simple (my guess is that you only see the first monster because RTF ignores following RTF documents plainly attached to RTF content). Instead, you have to

  1. Create a Document instance for your destination file.
  2. Read the desired content from your source file(s) into javax.swing.text.Documents.
  3. Insert the content from the source Documents into the destination Document using the proper API.
  4. Write the destination Document to a new file.

As ControlAltDel commented, you can employ the RTFEditorKit for this (example code slightly adapted from Example 5 of http://www.programcreek.com/java-api-examples/index.php?api=javax.swing.text.rtf.RTFEditorKit):

/**
 * 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);
    }
}
Lars Gendner
  • 1,816
  • 2
  • 14
  • 24