|
@@ -19,18 +19,16 @@
|
19
|
19
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20
|
20
|
* SOFTWARE.
|
21
|
21
|
*/
|
22
|
|
-
|
23
|
22
|
package com.dmdirc.util.io;
|
24
|
23
|
|
25
|
24
|
import java.io.BufferedReader;
|
26
|
|
-import java.io.BufferedWriter;
|
27
|
25
|
import java.io.File;
|
28
|
|
-import java.io.FileInputStream;
|
29
|
|
-import java.io.FileWriter;
|
30
|
26
|
import java.io.IOException;
|
31
|
27
|
import java.io.InputStream;
|
32
|
28
|
import java.io.InputStreamReader;
|
33
|
29
|
import java.nio.charset.Charset;
|
|
30
|
+import java.nio.file.Files;
|
|
31
|
+import java.nio.file.Path;
|
34
|
32
|
import java.util.ArrayList;
|
35
|
33
|
import java.util.List;
|
36
|
34
|
|
|
@@ -39,13 +37,21 @@ import java.util.List;
|
39
|
37
|
*/
|
40
|
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
|
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
|
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
|
55
|
private final Charset charset;
|
50
|
56
|
|
51
|
57
|
/**
|
|
@@ -68,6 +74,16 @@ public class TextFile {
|
68
|
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
|
88
|
* Creates a new instance of TextFile for an input stream, and uses the
|
73
|
89
|
* default charset.
|
|
@@ -79,21 +95,32 @@ public class TextFile {
|
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
|
101
|
* @param file The file to read
|
86
|
102
|
* @param charset The charset to read the file in
|
87
|
103
|
* @since 0.6.3m1
|
88
|
104
|
*/
|
89
|
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
|
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
|
125
|
* @param is The input stream to read from
|
99
|
126
|
* @param charset The charset to read the file in
|
|
@@ -125,25 +152,15 @@ public class TextFile {
|
125
|
152
|
* @throws IOException If an I/O exception occurs
|
126
|
153
|
*/
|
127
|
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
|
158
|
lines = new ArrayList<>();
|
137
|
|
-
|
138
|
159
|
String line;
|
139
|
160
|
|
140
|
161
|
while ((line = reader.readLine()) != null) {
|
141
|
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,7 +170,14 @@ public class TextFile {
|
153
|
170
|
* @return True if the file is writable, false otherwise
|
154
|
171
|
*/
|
155
|
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,23 +187,12 @@ public class TextFile {
|
163
|
187
|
* @throws IOException if an I/O exception occurs
|
164
|
188
|
*/
|
165
|
189
|
public void writeLines(final List<String> lines) throws IOException {
|
166
|
|
- if (file == null) {
|
|
190
|
+ if (path == null) {
|
167
|
191
|
throw new UnsupportedOperationException("Cannot write to TextFile "
|
168
|
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,19 +201,33 @@ public class TextFile {
|
188
|
201
|
* @return This TextFile's file, or null
|
189
|
202
|
*/
|
190
|
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
|
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
|
226
|
throw new UnsupportedOperationException("Cannot delete TextFile "
|
200
|
227
|
+ "opened with an InputStream");
|
201
|
228
|
}
|
202
|
229
|
|
203
|
|
- file.delete();
|
|
230
|
+ Files.delete(path);
|
204
|
231
|
}
|
205
|
232
|
|
206
|
233
|
}
|