|
25 | 25 |
|
26 | 26 | import java.io.BufferedReader; |
27 | 27 | import java.io.BufferedWriter; |
28 | | -import java.io.File; |
29 | | -import java.io.FileInputStream; |
30 | 28 | import java.io.FileNotFoundException; |
31 | | -import java.io.FileOutputStream; |
32 | | -import java.io.FileReader; |
33 | | -import java.io.FileWriter; |
34 | 29 | import java.io.IOException; |
35 | 30 | import java.io.InputStream; |
36 | 31 | import java.net.URL; |
37 | 32 | import java.net.URLConnection; |
38 | | -import java.nio.channels.FileChannel; |
39 | | -import java.util.ArrayList; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Path; |
40 | 35 | import java.util.Collection; |
41 | 36 | import java.util.HashMap; |
42 | 37 | import java.util.LinkedList; |
43 | 38 |
|
44 | | -public class FileUtil { |
| 39 | +public class PathUtil { |
45 | 40 | private static final HashMap<String, String> fileNameCache = new HashMap<>(); |
46 | 41 |
|
47 | 42 | /** |
48 | | - * Computes a long CRC of a File |
| 43 | + * Computes a long CRC of a Path |
49 | 44 | * |
50 | 45 | * @param file the file to process |
51 | | - * @param a buffer for temporary data |
| 46 | + * @param buffer a buffer for temporary data |
52 | 47 | * @return the CRC or 0 on failure |
53 | 48 | */ |
54 | | - public static long getCRC(File file, byte[] buffer) { |
55 | | - |
56 | | - FileInputStream in = null; |
57 | | - |
58 | | - try { |
59 | | - in = new FileInputStream(file); |
| 49 | + public static long getCRC(Path file, byte[] buffer) { |
| 50 | + try (InputStream in = Files.newInputStream(file)) { |
60 | 51 | return getCRC(in, buffer); |
61 | 52 | } catch (FileNotFoundException e) { |
62 | 53 | return 0; |
63 | | - } finally { |
64 | | - if (in != null) { |
65 | | - try { |
66 | | - in.close(); |
67 | | - } catch (IOException e) { |
68 | | - } |
69 | | - } |
| 54 | + } catch (IOException ex) { |
| 55 | + return 0; |
70 | 56 | } |
71 | 57 | } |
72 | 58 |
|
@@ -150,147 +136,48 @@ public static String getFileName(String url) { |
150 | 136 | } |
151 | 137 |
|
152 | 138 | /** |
153 | | - * Writes a Collection of Strings to a File, overwriting any previous file contents. |
| 139 | + * Writes a Collection of Strings to a Path, overwriting any previous file contents. |
154 | 140 | * |
155 | | - * Each String is converted into a line in the File. |
| 141 | + * Each String is converted into a line in the Path. |
156 | 142 | * |
157 | 143 | * @param strings the Collection of Strings |
158 | | - * @param file the file to write |
| 144 | + * @param path the file to write |
159 | 145 | * @return true on success |
160 | 146 | */ |
161 | | - public static boolean stringToFile(Collection<String> strings, File file) { |
162 | | - BufferedWriter bw; |
163 | | - |
164 | | - try { |
165 | | - bw = new BufferedWriter(new FileWriter(file)); |
166 | | - } catch (FileNotFoundException fnfe) { |
167 | | - return false; |
168 | | - } catch (IOException ioe) { |
169 | | - return false; |
170 | | - } |
| 147 | + public static boolean stringToFile(Collection<String> strings, Path path) { |
171 | 148 |
|
172 | | - try { |
| 149 | + try (BufferedWriter bw = Files.newBufferedWriter(path)) { |
173 | 150 | for (String line : strings) { |
174 | 151 | bw.write(line); |
175 | 152 | bw.newLine(); |
176 | 153 | } |
177 | 154 | return true; |
178 | 155 | } catch (IOException ioe) { |
179 | 156 | return false; |
180 | | - } finally { |
181 | | - try { |
182 | | - bw.close(); |
183 | | - } catch (IOException ioe2) { |
184 | | - } |
185 | 157 | } |
186 | 158 | } |
187 | 159 |
|
188 | 160 | /** |
189 | | - * Reads a File and places the contents into a collection of Strings. |
| 161 | + * Reads a Path and places the contents into a collection of Strings. |
190 | 162 | * |
191 | | - * Each line in the File is converted into a String. |
| 163 | + * Each line in the Path is converted into a String. |
192 | 164 | * |
193 | 165 | * Iterators on the List will iterate through the Strings in the order the lines appear in the file |
194 | 166 | * |
195 | 167 | * @param the file to read |
196 | 168 | * @return the Collection of Strings or null on failure |
197 | 169 | */ |
198 | | - public static Collection<String> fileToString(File file) { |
199 | | - |
200 | | - BufferedReader br; |
201 | | - |
202 | | - try { |
203 | | - br = new BufferedReader(new FileReader(file)); |
204 | | - } catch (FileNotFoundException fnfe) { |
205 | | - return null; |
206 | | - } |
207 | | - |
| 170 | + public static Collection<String> fileToString(Path path) { |
208 | 171 | String line; |
209 | 172 |
|
210 | | - try { |
| 173 | + try (BufferedReader br = Files.newBufferedReader(path)) { |
211 | 174 | Collection<String> strings = new LinkedList<>(); |
212 | 175 | while ((line = br.readLine()) != null) { |
213 | 176 | strings.add(line); |
214 | 177 | } |
215 | 178 | return strings; |
216 | 179 | } catch (IOException | NumberFormatException ioe) { |
217 | 180 | return null; |
218 | | - } finally { |
219 | | - try { |
220 | | - br.close(); |
221 | | - } catch (IOException ioe) { |
222 | | - } |
223 | 181 | } |
224 | 182 | } |
225 | | - |
226 | | - private static final Collection<String> emptyCollection = new ArrayList<>(); |
227 | | - |
228 | | - /** |
229 | | - * Creates a blank file |
230 | | - * |
231 | | - * @param the file to create |
232 | | - * @return true on success |
233 | | - */ |
234 | | - public static boolean createFile(File file) { |
235 | | - if (file == null) { |
236 | | - return false; |
237 | | - } |
238 | | - File dir = file.getParentFile(); |
239 | | - if (dir != null) { |
240 | | - if (!dir.exists()) { |
241 | | - if (!dir.mkdirs()) { |
242 | | - return false; |
243 | | - } |
244 | | - } else { |
245 | | - if (!dir.isDirectory()) { |
246 | | - return false; |
247 | | - } |
248 | | - } |
249 | | - } |
250 | | - return FileUtil.stringToFile(emptyCollection, file); |
251 | | - } |
252 | | - |
253 | | - /** |
254 | | - * Copies one file to another location |
255 | | - * |
256 | | - * @param inFile the source filename |
257 | | - * @param outFile the target filename |
258 | | - * @return true on success |
259 | | - */ |
260 | | - @SuppressWarnings ("resource") |
261 | | - public static boolean copy(File inFile, File outFile) { |
262 | | - if (!inFile.exists()) { |
263 | | - return false; |
264 | | - } |
265 | | - |
266 | | - FileChannel in = null; |
267 | | - FileChannel out = null; |
268 | | - |
269 | | - try { |
270 | | - in = new FileInputStream(inFile).getChannel(); |
271 | | - out = new FileOutputStream(outFile).getChannel(); |
272 | | - |
273 | | - long pos = 0; |
274 | | - long size = in.size(); |
275 | | - |
276 | | - while (pos < size) { |
277 | | - pos += in.transferTo(pos, 10 * 1024 * 1024, out); |
278 | | - } |
279 | | - } catch (IOException ioe) { |
280 | | - return false; |
281 | | - } finally { |
282 | | - try { |
283 | | - if (in != null) { |
284 | | - in.close(); |
285 | | - } |
286 | | - if (out != null) { |
287 | | - out.close(); |
288 | | - } |
289 | | - } catch (IOException ioe) { |
290 | | - return false; |
291 | | - } |
292 | | - } |
293 | | - |
294 | | - return true; |
295 | | - } |
296 | 183 | } |
0 commit comments