repast-simphony

Repast: how to overwrite the existing file rather than auto-create a new file in text sink


I wonder how to overwrite the existing file rather than auto-create a new file in text sink.

for instance, if I run the model two times it creates two files. The second file is created using the same defined file name but with a new number behind the name, e.g. modelouput.txt, modeloutput.0.txt, modeloutput.1.txt.

How to get rid of this auto version number creation and just overwrite the existing one if it exists.


Solution

  • There isn't an option in the Repast GUI FileSink to overwrite existing files, however you can just delete the file in your code. You can add the following code to your context builder that will delete the modeloutput.txt file at the beginning of the run so that the FileSink will always just make a new file with the same name:

    Path path_to_delete = Paths.get("output", "ModelOutput.txt");
    
        if (Files.exists(path_to_delete)) try {
            Files.delete(path_to_delete);
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    

    Alternatively, you could use Files.move(existingFilePath,newFilePath) to rename or move any existing output files to a different folder.