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

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