0

Im creating a java app in which some text is to be stored in a text file. But store function will run in a loop where every cycle will fetch data from other classes and store in the text file. I want that my text file should store data on each cycle just like you create log. here is some piece of code:

public void store(){
        File file = new File("PaperRecord.txt");

        try{
            PrintWriter fout = new PrintWriter(file);
            fout.println("Paper Name: " + super.getpSame());
            fout.println("Paper Size: " + super.getpSize());
            fout.println("Paper Year: " + super.getpYear());
            fout.println("Paper Author: " + super.getpAuthor());
            fout.println("Paper Description: " + getpDesc());
            fout.println("Paper Signature: " + getpSign());
            fout.println("Email: " + getPEmail());
            fout.println("");
        }
        catch(FileNotFoundException e){
            //do nothing
        }

    }

Calling store function from main using loop:

while(!q.isEmpty()){

                        Papers temp = q.remove();
                        temp.print();
                        temp.store();

                    }

THe problem currently with this code is that the code create new file paperrecord each time or overrite existing. I want the same file to be increased and updated downward (more text added)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    [java append to file](//stackoverflow.com/q/369760) – 001 May 07 '18 at 18:01
  • https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – NiVeR May 07 '18 at 18:02
  • `fout = new PrintWriter( new FileOutputStream( file, true) );` (not tested.) – markspace May 07 '18 at 18:02
  • P.S. Catching an exception and then doing nothing is almost always a bad idea. Please at least call `e.printStackTrace();` on it. `printStackTrace` is considered sub-optimal, but it's far better than nothing. – markspace May 07 '18 at 18:09
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson May 08 '18 at 06:03

1 Answers1

0

Files class is your friend dear.

try {
    Files.write(Paths.get("PaperRecord.txt"), "new text appended".getBytes(), StandardOpenOption.APPEND);
}catch (IOException e) {
    //exception handling left as an exercise for the reader
}

Or,a sample working code:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class AppendToFileExample {

    private static final String FILENAME = "E:\\test\\PaperRecord.txt";

    public static void main(String[] args) {

        BufferedWriter bw = null;
        FileWriter fw = null;

        try {

            String data = " This is new content";

            File file = new File(FILENAME);

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            // true = append file
            fw = new FileWriter(file.getAbsoluteFile(), true);
            bw = new BufferedWriter(fw);

            bw.write(data);

            System.out.println("Done");

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if (bw != null)
                    bw.close();

                if (fw != null)
                    fw.close();

            } catch (IOException ex) {

                ex.printStackTrace();

            }
        }

    }
}
beingmanish
  • 1,040
  • 8
  • 21