/src/org/ooc/backend/CachedFileWriter.java

http://github.com/nddrylliog/ooc · Java · 34 lines · 25 code · 9 blank · 0 comment · 3 complexity · 9d29fda6e067f598c1427180ecda1719 MD5 · raw file

  1. package org.ooc.backend;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.StringWriter;
  5. import org.ooc.utils.FileUtils;
  6. public class CachedFileWriter extends StringWriter {
  7. private File file;
  8. public CachedFileWriter(File file) {
  9. super();
  10. this.file = file;
  11. }
  12. @Override
  13. public void close() throws IOException {
  14. String thisContent = toString();
  15. if(!file.exists()) {
  16. FileUtils.write(file, thisContent);
  17. } else {
  18. String fileContent = FileUtils.read(file);
  19. if(!fileContent.equals(thisContent)) {
  20. FileUtils.write(file, thisContent);
  21. }
  22. }
  23. super.close();
  24. }
  25. }