Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2017 eyeo GmbH | |
4 * | |
5 * Adblock Plus is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
17 | |
18 package org.adblockplus.libadblockplus; | |
19 | |
20 import java.io.File; | |
21 import java.io.FileNotFoundException; | |
22 import java.io.FileOutputStream; | |
23 import java.io.IOException; | |
24 import java.util.NoSuchElementException; | |
25 import java.util.Scanner; | |
26 import java.util.UUID; | |
27 | |
28 public class FileSystemUtils | |
29 { | |
30 /** | |
31 * Read all the file data to string | |
32 * | |
33 * @param file path to read data | |
34 * @return file data | |
35 * @throws java.io.FileNotFoundException | |
36 */ | |
37 public static String readFile(File file) throws FileNotFoundException | |
38 { | |
39 Scanner scanner = null; | |
40 try | |
41 { | |
42 scanner = new Scanner(file); | |
43 return scanner.useDelimiter("\\A").next(); | |
sergei
2017/05/22 12:09:07
I'm not sure that it's the best way to read files
anton
2017/05/22 13:04:38
there are lots of ways for "reading file as string
| |
44 } | |
45 catch (NoSuchElementException e) | |
46 { | |
47 return ""; | |
48 } | |
49 finally | |
50 { | |
51 if (scanner != null) | |
52 { | |
53 scanner.close(); | |
54 } | |
55 } | |
56 } | |
57 | |
58 /** | |
59 * Write data to file (with rewriting) | |
60 * | |
61 * @param file file | |
62 * @param data file data | |
63 */ | |
64 public static void writeFile(File file, String data) throws IOException | |
65 { | |
66 FileOutputStream fos = null; | |
67 try | |
68 { | |
69 fos = new FileOutputStream(file); | |
70 if (data != null) | |
71 { | |
72 fos.write(data.getBytes()); | |
sergei
2017/05/22 12:09:07
If we still use String then I think we should pass
anton
2017/05/22 13:04:38
Acknowledged.
| |
73 } | |
74 } | |
75 finally | |
76 { | |
77 if (fos != null) | |
78 { | |
79 try | |
80 { | |
81 fos.close(); | |
82 } | |
83 catch (IOException e) | |
84 { | |
85 // ignored | |
86 } | |
87 } | |
88 } | |
89 } | |
90 | |
91 /** | |
92 * Generate unique filename | |
93 * | |
94 * @param prefix prefix | |
95 * @param suffix suffix | |
96 * @return generated unique filename | |
97 */ | |
98 public static String generateUniqueFileName(String prefix, String suffix) | |
99 { | |
100 StringBuilder sb = new StringBuilder(); | |
101 if (prefix != null) | |
102 { | |
103 sb.append(prefix); | |
104 } | |
105 | |
106 sb.append(UUID.randomUUID().toString()); | |
107 | |
108 if (suffix != null) | |
109 { | |
110 sb.append(suffix); | |
111 } | |
112 | |
113 return sb.toString(); | |
114 } | |
115 } | |
OLD | NEW |