2021年4月3日星期六

Write file content after reading is finish

Sometimes when my app is closed immediately after being initialized, some data is not saved, i have multiple data, i call save method onClose, so what is the best solution for this?, only write when reader has read all the file data?, i can make a lock for that or what is the best solution?

class Data {    private String data;    private final File file;    public Main(File file, String data) {      this.file = file;      this.data = data;  }      public void load() {      try (BufferedReader reader = Files.newBufferedReader(file.toPath())) {          data = new JsonParser().parse(reader).getAsString();      } catch (IOException e) {          e.printStackTrace();      }  }    public void save() {      try (BufferedWriter writer = Files.newBufferedWriter(file.toPath())) {          new GsonBuilder().create().toJson(data);      } catch (IOException e) {          e.printStackTrace();      }  }  

}

https://stackoverflow.com/questions/66937420/write-file-content-after-reading-is-finish April 04, 2021 at 10:03AM

没有评论:

发表评论