简体   繁体   中英

Java: Txt file resets with each execution of the code instead of saving modifications

I am new to Java, and i am experimenting with txt files. The following program allows for the user to access a set of activities, each with their own ID, and modify it according to their needs. (there is additional code which allows for the modification of the file). However, although at the end of the code the txt file is successfuly modified, each time the code is executed again, the txt file resets instead of keeping the modifications. Why is that? As can be seen i have added an if statement to prevent this.

package Try2;
import java.io.*;
import java.util.Scanner;

public class Activities {
    public static void main(String[] args) throws IOException {
        
        String acFileName = "./Activities.txt";

        File acFile = new File(acFileName);
        
        FileWriter acFw = new FileWriter(acFile);
        
        PrintWriter acPw = new PrintWriter(acFw);
        
        if((!acFile.exists()) || (acFile.length() == 0)) { //Checks if the file is empty/non existent (useful for the first time the code is executed)
            
            acPw.println("1 Sleep");
            acPw.println("2 Exercise");
            acPw.println("3 Socializing");
            acPw.println("4 Studying");
            acPw.println("5 Transportation");
            acPw.close();
        }

When you run the code:

FileWriter acFw = new FileWriter(acFile);

the file is truncated and any writes are started at the beginning of the file. One possible way of changing that code is:

String acFileName = "./Activities.txt";

File acFile = new File(acFileName);
    
if((!acFile.exists()) || (acFile.length() == 0)) { //Checks if the file is empty/non existent (useful for the first time the code is executed)
    FileWriter acFw = new FileWriter(acFile, true);
   
    PrintWriter acPw = new PrintWriter(acFw);

    acPw.println("1 Sleep");
    acPw.println("2 Exercise");
    acPw.println("3 Socializing");
    acPw.println("4 Studying");
    acPw.println("5 Transportation");
    acPw.close();
}

Note that I've move the creation of the FileWriter and PrintWriter inside the if code. Additionally, I've added a boolean to the construction of the FileWriter set to true . According to the docs :

public FileWriter(File file, boolean append) throws IOException

Constructs a FileWriter given the File to write and a boolean indicating whether to append the data written, using the platform's default charset.

Parameters: file - the File to write

append - if true, then bytes will be written to the end of the file rather than the beginning

The key is the append parameter. This flag means that you append each time you open the file.

You can simply provide your FileWriter with an append flag set to true (see below).

Without that flag, FileWriter creates a new file.

Your conditional logic is then superfluous, as the checking is done by the FileWriter itself:

public static void main(String[] args) throws IOException {

    String acFileName = "./Activities.txt";
    File acFile = new File(acFileName);
    PrintWriter acPw  = new PrintWriter(new FileWriter(acFile, true));

    acPw.println("1 Sleep");
    acPw.println("2 Exercise");
    acPw.println("3 Socializing");
    acPw.println("4 Studying");
    acPw.println("5 Transportation");
    acPw.close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM