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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.ArrayList;
  32. import java.util.List;
  33. /**
  34. * Allows reading and writing to a plain text file via a list of lines.
  35. */
  36. public class TextFile {
  37. /**
  38. * The file we're dealing with.
  39. */
  40. private Path path;
  41. /**
  42. * The input stream we're dealing with.
  43. */
  44. private InputStream is;
  45. /**
  46. * The lines we've read from the file.
  47. */
  48. private List<String> lines;
  49. /**
  50. * The charset to use to read the file.
  51. */
  52. private final Charset charset;
  53. /**
  54. * Creates a new instance of TextFile for the specified file, and uses the
  55. * default charset.
  56. *
  57. * @param filename The file to be read/written
  58. */
  59. public TextFile(final String filename) {
  60. this(new File(filename));
  61. }
  62. /**
  63. * Creates a new instance of TextFile for the specified File, and uses the
  64. * default charset.
  65. *
  66. * @param file The file to read
  67. */
  68. public TextFile(final File file) {
  69. this(file, Charset.defaultCharset());
  70. }
  71. /**
  72. * Creates a new instance of TextFile for the specified Path, and uses the
  73. * default charset.
  74. *
  75. * @param path The path to read
  76. */
  77. public TextFile(final Path path) {
  78. this(path, Charset.defaultCharset());
  79. }
  80. /**
  81. * Creates a new instance of TextFile for an input stream, and uses the
  82. * default charset.
  83. *
  84. * @param is The input stream to read from
  85. */
  86. public TextFile(final InputStream is) {
  87. this(is, Charset.defaultCharset());
  88. }
  89. /**
  90. * Creates a new instance of TextFile for the specified File, which is to be
  91. * read using the specified charset.
  92. *
  93. * @param file The file to read
  94. * @param charset The charset to read the file in
  95. * @since 0.6.3m1
  96. */
  97. public TextFile(final File file, final Charset charset) {
  98. this(file == null ? null : file.toPath(), charset);
  99. }
  100. /**
  101. * Creates a new instance of TextFile for the specified Path, which is to be
  102. * read using the specified charset.
  103. *
  104. * @param path The path to read
  105. * @param charset The charset to read the file in
  106. */
  107. public TextFile(final Path path, final Charset charset) {
  108. this.path = path;
  109. this.charset = charset;
  110. }
  111. /**
  112. * Creates a new instance of TextFile for an input stream, which is to be
  113. * read using the specified charset.
  114. *
  115. * @param is The input stream to read from
  116. * @param charset The charset to read the file in
  117. * @since 0.6.3m1
  118. */
  119. public TextFile(final InputStream is, final Charset charset) {
  120. this.is = is;
  121. this.charset = charset;
  122. }
  123. /**
  124. * Retrieves the contents of the file as a list of lines. If getLines() or
  125. * readLines() has previously been called, a cached version is returned.
  126. *
  127. * @return A list of lines in the file
  128. * @throws IOException if an I/O exception occurs
  129. */
  130. public List<String> getLines() throws IOException {
  131. if (lines == null) {
  132. readLines();
  133. }
  134. return lines;
  135. }
  136. /**
  137. * Reads the contents of the file into this TextFile's line cache.
  138. *
  139. * @throws IOException If an I/O exception occurs
  140. */
  141. public void readLines() throws IOException {
  142. try (BufferedReader reader = path == null
  143. ? new BufferedReader(new InputStreamReader(is, charset))
  144. : Files.newBufferedReader(path, charset)) {
  145. lines = new ArrayList<>();
  146. String line;
  147. while ((line = reader.readLine()) != null) {
  148. lines.add(line);
  149. }
  150. }
  151. }
  152. /**
  153. * Determines if this file is writable or not.
  154. *
  155. * @return True if the file is writable, false otherwise
  156. */
  157. public boolean isWritable() {
  158. if (path == null) {
  159. return false;
  160. }
  161. if (Files.exists(path)) {
  162. return Files.isWritable(path);
  163. } else {
  164. return Files.isWritable(path.getParent());
  165. }
  166. }
  167. /**
  168. * Writes the specified list of lines to the file.
  169. *
  170. * @param lines The lines to be written
  171. * @throws IOException if an I/O exception occurs
  172. */
  173. public void writeLines(final List<String> lines) throws IOException {
  174. if (path == null) {
  175. throw new UnsupportedOperationException("Cannot write to TextFile "
  176. + "opened with an InputStream");
  177. }
  178. Files.write(path, lines, charset);
  179. }
  180. /**
  181. * Retrieves the File for this TextFile, if there is one.
  182. *
  183. * @return This TextFile's file, or null
  184. */
  185. public File getFile() {
  186. if (path == null) {
  187. return null;
  188. }
  189. return path.toFile();
  190. }
  191. /**
  192. * Retrieves the Path for this TextFile, if there is one.
  193. *
  194. * @return This TextFile's path, or null
  195. */
  196. public Path getPath() {
  197. return path;
  198. }
  199. /**
  200. * Deletes the file associated with this textfile, if there is one.
  201. *
  202. * @throws IOException if the file is unable to be deleted
  203. */
  204. public void delete() throws IOException {
  205. if (path == null) {
  206. throw new UnsupportedOperationException("Cannot delete TextFile "
  207. + "opened with an InputStream");
  208. }
  209. Files.delete(path);
  210. }
  211. }