0

The first time I save a file, even if it exists, I get the result I want. The file is overwritten. Now If I save again the text is appended to the file instead of overwriting. I have tried applying the approaches from similar questions I found answered here but none solve this. This is not answered by the possible duplicate. When I save a file the first time the behavior is what I want, the file is overwritten. However, all subsequent saves with the java window open append. That is incorrect. I suspect the file may not be closing after writing.

Edits: This is a javafx package so the minimal amount of code to run is going to be a bit long but here it goes.

MainController:

public class MainController{

/** Holder of a switchable vista. */
@FXML public StackPane vistaHolder;

@FXML
public void programExit(){System.exit(0);}
public void permutationAnalyzer(){VistaNavigator.loadVista(VistaNavigator.VISTA_3);}

@FXML
public void saveFile() throws IOException {
    OptionsDataCollector.generateOptionsFile();
}

public void setVista(Node node) {
    vistaHolder.getChildren().setAll(node);
}
}

The data being saved:

public class OptionsDataCollector{
    public static void generateOptionsFile() throws IOException {
        StringBuilder moduleData = new StringBuilder();
        moduleData.append("--target_perm_group_size\t").append(targetGroupSize).append("\n");
        moduleData.append("--prog_check\t").append(progCheck).append("\n");
        FileSaveUtility.fileSaveWindow(moduleData.toString());
        //The next line is the answer.
        moduleData.setLength(0);
    }
}

FileSave:

public class FileSaveUtility {

private static void saveFile(String content, File file) throws IOException {

    FileWriter fileWriter = new FileWriter(file.getAbsoluteFile(), false);
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    bufferedWriter.write(content);
    bufferedWriter.close();

    //fileWriter.write(content);
    //fileWriter.close();
}

public static void fileSaveWindow(String outFileData) throws IOException {
    Stage primaryStage = new Stage();
    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("TXT files (*.txt)", "*.txt");
    fileChooser.getExtensionFilters().add(extFilter);
    fileChooser.setInitialFileName("run_Volundr.txt");
    File file = fileChooser.showSaveDialog(primaryStage);
    if(file != null){
        saveFile(outFileData, file);
        //primaryStage.close(); 
    }
}
}

I have not included the FXML files or the MainApp that generates it. I can add them if anyone wants. I have tried closing the stage, using StandardOpenOptions.Truncate_Exsisting and Files.deleteIfExsists. There is an object or stream not closing somewhere.

Dennis Simpson
  • 85
  • 1
  • 10
  • I am aware of that question. Mine is different. When not closing the java window the first save is correct, it overwrites. Subsequent saves all append which is incorrect. That is what I am trying to solve. – Dennis Simpson Jul 17 '16 at 17:18
  • Tunaki was quick to mark my question as a duplicate and close it out even though those questions are not relevant. Turns out this is a duplicate to this unanswered question: http://stackoverflow.com/questions/30739195/filewriter-will-only-append-not-overwrite?rq=1 – Dennis Simpson Jul 17 '16 at 17:30
  • 1
    The code you posted will overwrite the file contents with the string you provide. If it's not doing that, you have something else wrong in your code. [Edit] the question to include a [MCVE]. – James_D Jul 17 '16 at 17:54
  • The answer to this is the StringBuilder object was not clearing so the next time the user clicked File Save the new data was appended to the old. Thank you James_D, you got me thinking in the correct direction. I edited the code above to show the answer. At the risk of being annoying I will reiterate that this was not a duplicate as marked. – Dennis Simpson Jul 17 '16 at 20:20
  • Not a duplicate, but as the question was posted (at least originally), there was no way to answer it, since it didn't actually include the code that was making it fail. Consequently it probably should have been closed as [Off-Topic](http://stackoverflow.com/help/on-topic) (*"Questions seeking debugging help ('why isn't this code working?') must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself."*). It's not clear to me what the problem was even with the update: you appear to create a new `StringBuilder` each time. – James_D Jul 17 '16 at 20:39
  • @James_D I, incorrectly, thought the problem was with the file writing. That is why I did not include more code. I did intend it to create a new StringBuilder each time since the data could be coming from any of several screens and I thought that would clear any old data. I will be spending a bit of time to trace this better now that I understand where the error came from. – Dennis Simpson Jul 17 '16 at 20:56
  • Of course, if you actually knew what was causing the problem, you would be unlikely to be posting the question in the first place. This is a perfect example of why you should always include a [MCVE]. – James_D Jul 17 '16 at 20:58
  • Not having read all the code and comments, just guessing from the title: Could it be that you just have to change `new FileWriter(file.getAbsoluteFile(), false)` to `new FileWriter(file.getAbsoluteFile(), true)` ?! – Marco13 Jul 17 '16 at 21:26

0 Answers0