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

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