You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextFile.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.io;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.nio.charset.Charset;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.util.Collections;
  26. import java.util.List;
  27. import java.util.stream.Collectors;
  28. /**
  29. * Allows reading and writing to a plain text file via a list of lines.
  30. */
  31. public class TextFile {
  32. /**
  33. * The file we're dealing with.
  34. */
  35. private final Path path;
  36. /**
  37. * The input stream we're dealing with.
  38. */
  39. private final InputStream is;
  40. /**
  41. * The charset to use to read the file.
  42. */
  43. private final Charset charset;
  44. /**
  45. * The lines we've read from the file.
  46. */
  47. private List<String> lines;
  48. /**
  49. * Creates a new instance of TextFile for the specified Path, which is to be
  50. * read using the specified charset.
  51. *
  52. * @param path The path to read
  53. * @param charset The charset to read the file in
  54. */
  55. public TextFile(final Path path, final Charset charset) {
  56. this.path = path;
  57. this.is = null;
  58. this.charset = charset;
  59. }
  60. /**
  61. * Creates a new instance of TextFile for an input stream, which is to be
  62. * read using the specified charset.
  63. *
  64. * @param is The input stream to read from
  65. * @param charset The charset to read the file in
  66. * @since 0.6.3m1
  67. */
  68. public TextFile(final InputStream is, final Charset charset) {
  69. this.path = null;
  70. this.is = is;
  71. this.charset = charset;
  72. }
  73. /**
  74. * Retrieves the contents of the file as a list of lines. If getLines() or
  75. * readLines() has previously been called, a cached version is returned.
  76. *
  77. * @return An unmodifiable list of lines in the file
  78. * @throws IOException if an I/O exception occurs
  79. */
  80. public List<String> getLines() throws IOException {
  81. if (lines == null) {
  82. readLines();
  83. }
  84. return Collections.unmodifiableList(lines);
  85. }
  86. /**
  87. * Reads the contents of the file into this TextFile's line cache.
  88. *
  89. * @throws IOException If an I/O exception occurs
  90. */
  91. public void readLines() throws IOException {
  92. if (path == null) {
  93. try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset))) {
  94. lines = reader.lines().collect(Collectors.toList());
  95. }
  96. } else {
  97. lines = Files.readAllLines(path, charset);
  98. }
  99. }
  100. /**
  101. * Determines if this file is writable or not.
  102. *
  103. * @return True if the file is writable, false otherwise
  104. */
  105. public boolean isWritable() {
  106. if (path == null) {
  107. return false;
  108. }
  109. if (Files.exists(path)) {
  110. return Files.isWritable(path);
  111. } else {
  112. return Files.isWritable(path.getParent());
  113. }
  114. }
  115. /**
  116. * Writes the specified list of lines to the file.
  117. *
  118. * @param lines The lines to be written
  119. * @throws IOException if an I/O exception occurs
  120. */
  121. public void writeLines(final Iterable<String> lines) throws IOException {
  122. if (!isWritable()) {
  123. throw new UnsupportedOperationException("Cannot write to TextFile "
  124. + "opened with an InputStream");
  125. }
  126. Files.write(path, lines, charset);
  127. }
  128. /**
  129. * Deletes the file associated with this textfile, if there is one.
  130. *
  131. * @throws IOException if the file is unable to be deleted
  132. */
  133. public void delete() throws IOException {
  134. if (!isWritable()) {
  135. throw new UnsupportedOperationException("Cannot delete TextFile "
  136. + "opened with an InputStream");
  137. }
  138. Files.delete(path);
  139. }
  140. }