Document.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package rocks.zipcode;
  2. import java.io.*;
  3. import java.nio.charset.Charset;
  4. import java.nio.charset.StandardCharsets;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. import java.util.stream.Stream;
  11. /**
  12. * @author leon on 16/11/2018.
  13. */
  14. public class Document implements DocumentInterface {
  15. private final FileWriter fileWriter;
  16. private final File file;
  17. public Document(String fileName) throws IOException {
  18. this(new File(fileName));
  19. }
  20. public Document(File file) throws IOException {
  21. this.file = file;
  22. this.fileWriter = new FileWriter(file);
  23. }
  24. @Override
  25. public void write(String contentToBeWritten) throws IOException{
  26. BufferedWriter writer = new BufferedWriter(new FileWriter(file));
  27. writer.write(contentToBeWritten);
  28. writer.close();
  29. }
  30. @Override
  31. public void write(Integer lineNumber, String valueToBeWritten) throws IOException {
  32. List <String> lines = null;
  33. try {
  34. lines = Files.readAllLines(file.toPath());
  35. lines.set(lineNumber,valueToBeWritten);
  36. Files.write(file.toPath(), lines);
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. @Override
  42. public String read(Integer lineNumber) throws IOException {
  43. try(Stream<String> lines = Files.lines(Paths.get("target/file.txt"))){
  44. String line = lines.skip(lineNumber).findFirst().get();
  45. return line;
  46. }
  47. }
  48. @Override
  49. public String read() {
  50. StringBuilder builder = new StringBuilder();
  51. try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  52. String sCurrentLine;
  53. while ((sCurrentLine = br.readLine()) != null) {
  54. builder.append(sCurrentLine).append("\n");
  55. }
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. return builder.toString().trim();
  60. }
  61. @Override
  62. public void replaceAll(String stringToReplace, String replacementString) {
  63. Path path = Paths.get("target/file.txt");
  64. Charset charset = StandardCharsets.UTF_8;
  65. String content = null;
  66. try {
  67. content = new String(Files.readAllBytes(path), charset);
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. content = content.replaceAll(stringToReplace, replacementString);
  72. try {
  73. Files.write(path, content.getBytes(charset));
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. @Override
  79. public void overWrite(String content) {
  80. try {
  81. Writer fileWriter = new FileWriter("target/file.txt", false);
  82. write(content);
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. public List<String> toList() {
  88. return null;
  89. }
  90. @Override
  91. public File getFile() {
  92. return null;
  93. }
  94. @Override
  95. public String toString() {
  96. return null;
  97. }
  98. }