瀏覽代碼

Add Path support to TextFile.

Change-Id: I752f5bebe42514f1dc21e1b77e1ee5a649f324f5
Depends-On: I9463e66dc39881bcc69b64b8900df1010683b0bc
Depends-On: I752f5bebe42514f1dc21e1b77e1ee5a649f324f5
Depends-On: I4fa54ebe8f4ff93c8fc33504af66338eda01d0b8
Reviewed-on: http://gerrit.dmdirc.com/3502
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/02/3502/10
Greg Holmes 10 年之前
父節點
當前提交
3f4f28c3cc
共有 1 個文件被更改,包括 72 次插入45 次删除
  1. 72
    45
      src/com/dmdirc/util/io/TextFile.java

+ 72
- 45
src/com/dmdirc/util/io/TextFile.java 查看文件

19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
  * SOFTWARE.
20
  * SOFTWARE.
21
  */
21
  */
22
-
23
 package com.dmdirc.util.io;
22
 package com.dmdirc.util.io;
24
 
23
 
25
 import java.io.BufferedReader;
24
 import java.io.BufferedReader;
26
-import java.io.BufferedWriter;
27
 import java.io.File;
25
 import java.io.File;
28
-import java.io.FileInputStream;
29
-import java.io.FileWriter;
30
 import java.io.IOException;
26
 import java.io.IOException;
31
 import java.io.InputStream;
27
 import java.io.InputStream;
32
 import java.io.InputStreamReader;
28
 import java.io.InputStreamReader;
33
 import java.nio.charset.Charset;
29
 import java.nio.charset.Charset;
30
+import java.nio.file.Files;
31
+import java.nio.file.Path;
34
 import java.util.ArrayList;
32
 import java.util.ArrayList;
35
 import java.util.List;
33
 import java.util.List;
36
 
34
 
39
  */
37
  */
40
 public class TextFile {
38
 public class TextFile {
41
 
39
 
42
-    /** The file we're dealing with. */
43
-    private File file;
44
-    /** The input stream we're dealing with. */
40
+    /**
41
+     * The file we're dealing with.
42
+     */
43
+    private Path path;
44
+    /**
45
+     * The input stream we're dealing with.
46
+     */
45
     private InputStream is;
47
     private InputStream is;
46
-    /** The lines we've read from the file. */
48
+    /**
49
+     * The lines we've read from the file.
50
+     */
47
     private List<String> lines;
51
     private List<String> lines;
48
-    /** The charset to use to read the file. */
52
+    /**
53
+     * The charset to use to read the file.
54
+     */
49
     private final Charset charset;
55
     private final Charset charset;
50
 
56
 
51
     /**
57
     /**
68
         this(file, Charset.defaultCharset());
74
         this(file, Charset.defaultCharset());
69
     }
75
     }
70
 
76
 
77
+    /**
78
+     * Creates a new instance of TextFile for the specified Path, and uses the
79
+     * default charset.
80
+     *
81
+     * @param path The path to read
82
+     */
83
+    public TextFile(final Path path) {
84
+        this(path, Charset.defaultCharset());
85
+    }
86
+
71
     /**
87
     /**
72
      * Creates a new instance of TextFile for an input stream, and uses the
88
      * Creates a new instance of TextFile for an input stream, and uses the
73
      * default charset.
89
      * default charset.
79
     }
95
     }
80
 
96
 
81
     /**
97
     /**
82
-     * Creates a new instance of TextFile for the specified File, which is to
83
-     * be read using the specified charset.
98
+     * Creates a new instance of TextFile for the specified File, which is to be
99
+     * read using the specified charset.
84
      *
100
      *
85
      * @param file The file to read
101
      * @param file The file to read
86
      * @param charset The charset to read the file in
102
      * @param charset The charset to read the file in
87
      * @since 0.6.3m1
103
      * @since 0.6.3m1
88
      */
104
      */
89
     public TextFile(final File file, final Charset charset) {
105
     public TextFile(final File file, final Charset charset) {
90
-        this.file = file;
106
+        this(file.toPath(), charset);
107
+    }
108
+
109
+    /**
110
+     * Creates a new instance of TextFile for the specified Path, which is to be
111
+     * read using the specified charset.
112
+     *
113
+     * @param path The path to read
114
+     * @param charset The charset to read the file in
115
+     */
116
+    public TextFile(final Path path, final Charset charset) {
117
+        this.path = path;
91
         this.charset = charset;
118
         this.charset = charset;
92
     }
119
     }
93
 
120
 
94
     /**
121
     /**
95
-     * Creates a new instance of TextFile for an input stream, which is to
96
-     * be read using the specified charset.
122
+     * Creates a new instance of TextFile for an input stream, which is to be
123
+     * read using the specified charset.
97
      *
124
      *
98
      * @param is The input stream to read from
125
      * @param is The input stream to read from
99
      * @param charset The charset to read the file in
126
      * @param charset The charset to read the file in
125
      * @throws IOException If an I/O exception occurs
152
      * @throws IOException If an I/O exception occurs
126
      */
153
      */
127
     public void readLines() throws IOException {
154
     public void readLines() throws IOException {
128
-        BufferedReader reader = null;
129
-        InputStreamReader inputReader = null;
130
-        InputStream inputStream = null;
131
-
132
-        try {
133
-            inputStream = file == null ? is : new FileInputStream(file);
134
-            inputReader = new InputStreamReader(inputStream, charset);
135
-            reader = new BufferedReader(inputReader);
155
+        try (BufferedReader reader = path == null
156
+                ? new BufferedReader(new InputStreamReader(is, charset))
157
+                : Files.newBufferedReader(path, charset)) {
136
             lines = new ArrayList<>();
158
             lines = new ArrayList<>();
137
-
138
             String line;
159
             String line;
139
 
160
 
140
             while ((line = reader.readLine()) != null) {
161
             while ((line = reader.readLine()) != null) {
141
                 lines.add(line);
162
                 lines.add(line);
142
             }
163
             }
143
-        } finally {
144
-            StreamUtils.close(reader);
145
-            StreamUtils.close(inputReader);
146
-            StreamUtils.close(inputStream);
147
         }
164
         }
148
     }
165
     }
149
 
166
 
153
      * @return True if the file is writable, false otherwise
170
      * @return True if the file is writable, false otherwise
154
      */
171
      */
155
     public boolean isWritable() {
172
     public boolean isWritable() {
156
-        return file != null;
173
+        if (path == null) {
174
+            return false;
175
+        }
176
+        if (Files.exists(path)) {
177
+            return Files.isWritable(path);
178
+        } else {
179
+            return Files.isWritable(path.getParent());
180
+        }
157
     }
181
     }
158
 
182
 
159
     /**
183
     /**
163
      * @throws IOException if an I/O exception occurs
187
      * @throws IOException if an I/O exception occurs
164
      */
188
      */
165
     public void writeLines(final List<String> lines) throws IOException {
189
     public void writeLines(final List<String> lines) throws IOException {
166
-        if (file == null) {
190
+        if (path == null) {
167
             throw new UnsupportedOperationException("Cannot write to TextFile "
191
             throw new UnsupportedOperationException("Cannot write to TextFile "
168
                     + "opened with an InputStream");
192
                     + "opened with an InputStream");
169
         }
193
         }
170
 
194
 
171
-        BufferedWriter writer = null;
172
-
173
-        try {
174
-            writer = new BufferedWriter(new FileWriter(file));
175
-
176
-            for (String line : lines) {
177
-                writer.write(line);
178
-                writer.newLine();
179
-            }
180
-        } finally {
181
-            StreamUtils.close(writer);
182
-        }
195
+        Files.write(path, lines, charset);
183
     }
196
     }
184
 
197
 
185
     /**
198
     /**
188
      * @return This TextFile's file, or null
201
      * @return This TextFile's file, or null
189
      */
202
      */
190
     public File getFile() {
203
     public File getFile() {
191
-        return file;
204
+        if (path == null) {
205
+            return null;
206
+        }
207
+        return path.toFile();
208
+    }
209
+
210
+    /**
211
+     * Retrieves the Path for this TextFile, if there is one.
212
+     *
213
+     * @return This TextFile's path, or null
214
+     */
215
+    public Path getPath() {
216
+        return path;
192
     }
217
     }
193
 
218
 
194
     /**
219
     /**
195
      * Deletes the file associated with this textfile, if there is one.
220
      * Deletes the file associated with this textfile, if there is one.
221
+     *
222
+     * @throws IOException if the file is unable to be deleted
196
      */
223
      */
197
-    public void delete() {
198
-        if (file == null) {
224
+    public void delete() throws IOException {
225
+        if (path == null) {
199
             throw new UnsupportedOperationException("Cannot delete TextFile "
226
             throw new UnsupportedOperationException("Cannot delete TextFile "
200
                     + "opened with an InputStream");
227
                     + "opened with an InputStream");
201
         }
228
         }
202
 
229
 
203
-        file.delete();
230
+        Files.delete(path);
204
     }
231
     }
205
 
232
 
206
 }
233
 }

Loading…
取消
儲存