0

I am trying to make a simple program that will constantly add content to a text file, the problem with the code that I have so far is that have the previous content erased over the new content that I am trying to save. What I need to change to make my program add content and not to delete the previous one. Here is my class that I wrote so far...

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

public class TxtWriter{
private String content;
private File file;
private FileWriter fw;

//constractor
public TxtWriter(){
    content += "";
    file = new File("C:/Users/Geroge/SkyDrive/Documents/Java programs/FileWriter/inputFile.txt");
}
//write method
public void writeToFile(String date,double duration,double brakeDur){
    try {

        String content = "DATE: " + date + "| Shift duration: " + duration + "| brakeDur: " + brakeDur + "| Total hours: " + (duration - brakeDur) + "\n";

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


        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(content);
        bw.newLine();
        bw.close();

        System.out.println("Done");

    } catch (IOException e) {
        e.printStackTrace();
    }
}//end of writeToFile method

}

Engineer2021
  • 3,288
  • 6
  • 29
  • 51
Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
  • 1
    possible duplicate of [How to append text to an existing file in Java](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – m0skit0 Apr 05 '14 at 18:32
  • @m0skit0 no, not in 2014; since 2011 there is a much better way of doing what the possible duplicate mentions – fge Apr 05 '14 at 18:39

2 Answers2

3

Use FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);

FileWriter(File file, boolean append)
Constructs a FileWriter object given a File object.
Parameters:
fileName - String The system-dependent filename.
append - boolean if true, then data will be written to the end of the file rather than the beginning.

from here.

Rey Libutan
  • 5,226
  • 9
  • 42
  • 73
  • 1
    Java 8 is out, so at least promote `Files` which is now several years old... And so vastly superior to `File` that there is simply no comparison – fge Apr 05 '14 at 18:36
  • Thank you so much! so all I needed was true in the constructor of the fw object! :) – Georgi Koemdzhiev Apr 05 '14 at 18:41
  • 1
    @BogGogo use the new API. Seriously. It allows for a much saner, and more secure, file manipulation – fge Apr 05 '14 at 18:49
1

Use the new file API!

In your constructor, declare a Path, not a File:

targetFile = Paths.get("C:/Users/Geroge/SkyDrive/Documents/Java programs/FileWriter/inputFile.txt");

In your function which appends:

try (
    // Note the options: create if not exists; append if exists
    final BufferedWriter = Files.newBufferedWriter(targetFile, StandardCharsets.UTF_8,
        StandardOpenOption.CREATE, StandardOpenOption.APPEND);
) {
    // write contents to file
} // Automatically closed for you here

Drop File!

fge
  • 119,121
  • 33
  • 254
  • 329