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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.nio.charset.Charset;
  28. import java.nio.file.Files;
  29. import java.nio.file.Path;
  30. import java.util.Collections;
  31. import java.util.List;
  32. import java.util.stream.Collectors;
  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 final Path path;
  41. /**
  42. * The input stream we're dealing with.
  43. */
  44. private final InputStream is;
  45. /**
  46. * The charset to use to read the file.
  47. */
  48. private final Charset charset;
  49. /**
  50. * The lines we've read from the file.
  51. */
  52. private List<String> lines;
  53. /**
  54. * Creates a new instance of TextFile for the specified Path, which is to be
  55. * read using the specified charset.
  56. *
  57. * @param path The path to read
  58. * @param charset The charset to read the file in
  59. */
  60. public TextFile(final Path path, final Charset charset) {
  61. this.path = path;
  62. this.is = null;
  63. this.charset = charset;
  64. }
  65. /**
  66. * Creates a new instance of TextFile for an input stream, which is to be
  67. * read using the specified charset.
  68. *
  69. * @param is The input stream to read from
  70. * @param charset The charset to read the file in
  71. * @since 0.6.3m1
  72. */
  73. public TextFile(final InputStream is, final Charset charset) {
  74. this.path = null;
  75. this.is = is;
  76. this.charset = charset;
  77. }
  78. /**
  79. * Retrieves the contents of the file as a list of lines. If getLines() or
  80. * readLines() has previously been called, a cached version is returned.
  81. *
  82. * @return An unmodifiable list of lines in the file
  83. * @throws IOException if an I/O exception occurs
  84. */
  85. public List<String> getLines() throws IOException {
  86. if (lines == null) {
  87. readLines();
  88. }
  89. return Collections.unmodifiableList(lines);
  90. }
  91. /**
  92. * Reads the contents of the file into this TextFile's line cache.
  93. *
  94. * @throws IOException If an I/O exception occurs
  95. */
  96. public void readLines() throws IOException {
  97. if (path == null) {
  98. try (BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset))) {
  99. lines = reader.lines().collect(Collectors.toList());
  100. }
  101. } else {
  102. lines = Files.readAllLines(path, charset);
  103. }
  104. }
  105. /**
  106. * Determines if this file is writable or not.
  107. *
  108. * @return True if the file is writable, false otherwise
  109. */
  110. public boolean isWritable() {
  111. if (path == null) {
  112. return false;
  113. }
  114. if (Files.exists(path)) {
  115. return Files.isWritable(path);
  116. } else {
  117. return Files.isWritable(path.getParent());
  118. }
  119. }
  120. /**
  121. * Writes the specified list of lines to the file.
  122. *
  123. * @param lines The lines to be written
  124. * @throws IOException if an I/O exception occurs
  125. */
  126. public void writeLines(final Iterable<String> lines) throws IOException {
  127. if (!isWritable()) {
  128. throw new UnsupportedOperationException("Cannot write to TextFile "
  129. + "opened with an InputStream");
  130. }
  131. Files.write(path, lines, charset);
  132. }
  133. /**
  134. * Deletes the file associated with this textfile, if there is one.
  135. *
  136. * @throws IOException if the file is unable to be deleted
  137. */
  138. public void delete() throws IOException {
  139. if (!isWritable()) {
  140. throw new UnsupportedOperationException("Cannot delete TextFile "
  141. + "opened with an InputStream");
  142. }
  143. Files.delete(path);
  144. }
  145. }