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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2006-2015 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, which is to be
  56. * read using the specified charset.
  57. *
  58. * @param file The file to read
  59. * @param charset The charset to read the file in
  60. * @since 0.6.3m1
  61. */
  62. public TextFile(final File file, final Charset charset) {
  63. this(file == null ? null : file.toPath(), charset);
  64. }
  65. /**
  66. * Creates a new instance of TextFile for the specified Path, which is to be
  67. * read using the specified charset.
  68. *
  69. * @param path The path to read
  70. * @param charset The charset to read the file in
  71. */
  72. public TextFile(final Path path, final Charset charset) {
  73. this.path = path;
  74. this.is = null;
  75. this.charset = charset;
  76. }
  77. /**
  78. * Creates a new instance of TextFile for an input stream, which is to be
  79. * read using the specified charset.
  80. *
  81. * @param is The input stream to read from
  82. * @param charset The charset to read the file in
  83. * @since 0.6.3m1
  84. */
  85. public TextFile(final InputStream is, final Charset charset) {
  86. this.path = null;
  87. this.is = is;
  88. this.charset = charset;
  89. }
  90. /**
  91. * Retrieves the contents of the file as a list of lines. If getLines() or
  92. * readLines() has previously been called, a cached version is returned.
  93. *
  94. * @return An unmodifiable list of lines in the file
  95. * @throws IOException if an I/O exception occurs
  96. */
  97. public List<String> getLines() throws IOException {
  98. if (lines == null) {
  99. readLines();
  100. }
  101. return Collections.unmodifiableList(lines);
  102. }
  103. /**
  104. * Reads the contents of the file into this TextFile's line cache.
  105. *
  106. * @throws IOException If an I/O exception occurs
  107. */
  108. public void readLines() throws IOException {
  109. if (path == null) {
  110. try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset))) {
  111. lines = reader.lines().collect(Collectors.toList());
  112. }
  113. } else {
  114. lines = Files.readAllLines(path, charset);
  115. }
  116. }
  117. /**
  118. * Determines if this file is writable or not.
  119. *
  120. * @return True if the file is writable, false otherwise
  121. */
  122. public boolean isWritable() {
  123. if (path == null) {
  124. return false;
  125. }
  126. if (Files.exists(path)) {
  127. return Files.isWritable(path);
  128. } else {
  129. return Files.isWritable(path.getParent());
  130. }
  131. }
  132. /**
  133. * Writes the specified list of lines to the file.
  134. *
  135. * @param lines The lines to be written
  136. * @throws IOException if an I/O exception occurs
  137. */
  138. public void writeLines(final Iterable<String> lines) throws IOException {
  139. if (!isWritable()) {
  140. throw new UnsupportedOperationException("Cannot write to TextFile "
  141. + "opened with an InputStream");
  142. }
  143. Files.write(path, lines, charset);
  144. }
  145. /**
  146. * Deletes the file associated with this textfile, if there is one.
  147. *
  148. * @throws IOException if the file is unable to be deleted
  149. */
  150. public void delete() throws IOException {
  151. if (!isWritable()) {
  152. throw new UnsupportedOperationException("Cannot delete TextFile "
  153. + "opened with an InputStream");
  154. }
  155. Files.delete(path);
  156. }
  157. }