4 Replies Latest reply: Sep 16, 2007 10:59 PM by 807600 RSS

    Appending file with formatted output.

    807600
      I am trying to append a .txt file with output that is formatted using the Formatter class. However I can not figure out how I would do this. I have only been able to do it with unformatted output using the FileWriter class. I've looked around but couldn't find anything. Is it possible with this class, and if not is there any class I could use to do it.

      Edited by: Rhino on Sep 16, 2007 4:38 PM
        • 1. Re: Appending file with formatted output.
          807600
          Rhino wrote:
          I am trying to append a .txt file with output that is formatted using the Formatter class. However I can not figure out how I would do this. I have only been able to do it with unformatted output using the FileWriter class. I've looked around but couldn't find anything. Is it possible with this class, and if not is there any class I could use to do it.
          What exactly do you mean by "formatted"? do you mean formatting numeric data? No problem. Do you mean bold text, different fonts, italicized,... then BIG problem as this is impossible with vanilla txt files.
          • 2. Re: Appending file with formatted output.
            807600
            I guess it is too much to expect for you to explain how you are attempting this, what exact problems you are having or maybe even some code (wrap in code tags).
            • 3. Re: Appending file with formatted output.
              807600
              Here is a snippet of the code I am using currently. It outputs fine but it will just overwrite the file each time rather than add to it.
                        try{
                             Date timeStamp = new Date();
                             File file = new File("Invoice_Records.txt");
                             Formatter formatter = new Formatter(file);
                             formatter.format("\n\nINVOICE || ");
                             formatter.format(timeStamp.toGMTString() + "\n");
                             formatter.format("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n");
                             formatter.format("\n.:: Part Number%sDescription%sQuantity%sPrice%sTotal\n",
                                         invoice1.PtoDSpace(), invoice1.DtoQSpace(), invoice1.QtoPSpace(), invoice1.PtoTSpace());
                             formatter.format("----------------------------------------------------------------------------------------------------------------------------\n");
                             formatter.format("\n::: %s           %s           %d        $%.2f    $%.2f",
                                       invoice1.getPartNumber(), invoice1.getPartDescription(), invoice1.getPartQuantity(), invoice1.getPartPrice(), invoice1.getInvoiceAmount());
                             formatter.close();
                        } catch(FileNotFoundException e){
              Edited by: Rhino on Sep 16, 2007 8:37 PM

              Edited by: Rhino on Sep 16, 2007 8:43 PM
              • 4. Re: Appending file with formatted output.
                807600
                Have you looked at the Formatter API documentation?
                http://java.sun.com/javase/6/docs/api/java/util/Formatter.html

                Describing the constructor you are using it says: {color:#2020FF}Parameters:
                file - The file to use as the destination of this formatter. If the file exists then it
                will be truncated to zero size; otherwise, a new file will be created. The output
                will be written to the file and is buffered.
                {color}

                Consider the constructor whose argument is an OutputStream. In particular
                use an "appendable" FileOutputStream. See
                http://java.sun.com/javase/6/docs/api/java/io/FileOutputStream.html#FileOutputStream(java.lang.String, boolean)