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 6.3KB

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