16 |
16 |
import java.io.UnsupportedEncodingException;
|
17 |
17 |
import java.net.MalformedURLException;
|
18 |
18 |
import java.net.URL;
|
|
19 |
import java.net.URLConnection;
|
19 |
20 |
import java.util.ArrayList;
|
20 |
21 |
import java.util.Arrays;
|
21 |
22 |
import java.util.List;
|
... | ... | |
31 |
32 |
public static final String UTF8 = "UTF-8"; //$NON-NLS-1$
|
32 |
33 |
|
33 |
34 |
public static ArrayList<String> findWithGroup(File file, String pattern) throws IOException {
|
|
35 |
|
34 |
36 |
return findWithGroup(file, UTF8, pattern, false);
|
35 |
37 |
}
|
36 |
38 |
|
37 |
39 |
public static ArrayList<String> findWithGroup(File file, String pattern, boolean normalizeSeparators) throws IOException {
|
|
40 |
|
38 |
41 |
return findWithGroup(file, UTF8, pattern, normalizeSeparators);
|
39 |
42 |
}
|
40 |
43 |
|
41 |
44 |
public static ArrayList<String> findWithGroup(File file, String encoding, String pattern) throws IOException {
|
|
45 |
|
42 |
46 |
return findWithGroup(file, encoding, pattern, false);
|
43 |
47 |
}
|
44 |
48 |
|
... | ... | |
57 |
61 |
* @return a list containing the files not hidden
|
58 |
62 |
*/
|
59 |
63 |
public List<File> listFiles(File dir) {
|
|
64 |
|
60 |
65 |
File[] files = dir.listFiles(HIDDENFILE_FILTER);
|
61 |
66 |
if (files == null)
|
62 |
67 |
return new ArrayList<File>();
|
... | ... | |
65 |
70 |
}
|
66 |
71 |
|
67 |
72 |
public static ArrayList<String> findWithGroup(File file, String encoding, String pattern, boolean normalizeSeparators) throws IOException {
|
|
73 |
|
68 |
74 |
ArrayList<String> matches = new ArrayList<String>();
|
69 |
75 |
String text = IOUtils.getText(file, encoding);
|
70 |
76 |
if (normalizeSeparators) {
|
... | ... | |
83 |
89 |
}
|
84 |
90 |
|
85 |
91 |
public static ArrayList<String> find(File file, String pattern) throws IOException {
|
|
92 |
|
86 |
93 |
return find(file, UTF8, pattern, false);
|
87 |
94 |
}
|
88 |
95 |
|
89 |
96 |
public static ArrayList<String> find(File file, String pattern, boolean normalizeSeparators) throws IOException {
|
|
97 |
|
90 |
98 |
return findWithGroup(file, UTF8, pattern, normalizeSeparators);
|
91 |
99 |
}
|
92 |
100 |
|
93 |
101 |
public static ArrayList<String> find(File file, String encoding, String pattern) throws IOException {
|
|
102 |
|
94 |
103 |
return find(file, encoding, pattern, false);
|
95 |
104 |
}
|
96 |
105 |
|
97 |
106 |
public static ArrayList<String> find(File file, String encoding, String pattern, boolean normalizeSeparators) throws IOException {
|
|
107 |
|
98 |
108 |
ArrayList<String> matches = new ArrayList<String>();
|
99 |
109 |
String text = IOUtils.getText(file, encoding);
|
100 |
110 |
// System.out.println(text);
|
... | ... | |
114 |
124 |
}
|
115 |
125 |
|
116 |
126 |
public static BufferedReader getReader(File file) throws UnsupportedEncodingException, FileNotFoundException {
|
|
127 |
|
117 |
128 |
return getReader(file, UTF8);
|
118 |
129 |
}
|
119 |
130 |
|
... | ... | |
127 |
138 |
* @throws FileNotFoundException
|
128 |
139 |
*/
|
129 |
140 |
public static BufferedReader getReader(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException {
|
130 |
|
if (encoding == null)
|
131 |
|
encoding = "UTF-8"; // prevent null exception
|
132 |
|
|
|
141 |
|
|
142 |
if (encoding == null) {
|
|
143 |
encoding = UTF8; // prevent null exception
|
|
144 |
}
|
|
145 |
|
133 |
146 |
FileInputStream input = new FileInputStream(file);
|
134 |
147 |
DetectBOM db = new DetectBOM(file);
|
135 |
|
for (int ibom = 0; ibom < db.getBOMSize(); ibom++)
|
|
148 |
for (int ibom = 0; ibom < db.getBOMSize(); ibom++) {
|
136 |
149 |
try {
|
137 |
150 |
input.read();
|
138 |
151 |
} catch (IOException e) {
|
139 |
152 |
e.printStackTrace();
|
140 |
153 |
}
|
|
154 |
}
|
141 |
155 |
return new BufferedReader(new InputStreamReader(input, encoding));
|
142 |
156 |
}
|
143 |
157 |
|
144 |
158 |
public static BufferedReader getReader(String file) throws UnsupportedEncodingException, FileNotFoundException {
|
|
159 |
|
145 |
160 |
return getReader(new File(file), UTF8);
|
146 |
161 |
}
|
147 |
162 |
|
148 |
163 |
public static BufferedReader getReader(URL url) throws IOException {
|
|
164 |
|
149 |
165 |
return getReader(url, UTF8);
|
150 |
166 |
}
|
151 |
167 |
|
152 |
168 |
public static BufferedReader getReader(URL url, String encoding) throws IOException {
|
153 |
|
InputStream inputStream = url.openConnection().getInputStream();
|
|
169 |
|
|
170 |
if (encoding == null) {
|
|
171 |
encoding = UTF8; // prevent null exception
|
|
172 |
}
|
|
173 |
|
|
174 |
URLConnection conn = url.openConnection();
|
|
175 |
InputStream inputStream = conn.getInputStream();
|
154 |
176 |
return new BufferedReader(new InputStreamReader(inputStream, encoding));
|
155 |
177 |
}
|
156 |
178 |
|
157 |
179 |
public static void setText(File file, String text, String encoding) throws IOException {
|
|
180 |
|
158 |
181 |
PrintWriter writer = getWriter(file, encoding);
|
159 |
182 |
writer.print(text);
|
160 |
183 |
writer.close();
|
161 |
184 |
}
|
162 |
185 |
|
163 |
186 |
public static String getText(URL url, String encoding) throws IOException {
|
|
187 |
|
164 |
188 |
BufferedReader reader = getReader(url, encoding);
|
165 |
189 |
StringBuilder builder = new StringBuilder();
|
166 |
190 |
try {
|
... | ... | |
178 |
202 |
}
|
179 |
203 |
|
180 |
204 |
public static String getText(File file) throws IOException {
|
|
205 |
|
181 |
206 |
return getText(file, UTF8);
|
182 |
207 |
}
|
183 |
208 |
|
184 |
209 |
public static String getText(File file, String encoding) throws IOException {
|
|
210 |
|
185 |
211 |
BufferedReader reader = getReader(file, encoding);
|
186 |
212 |
StringBuilder builder = new StringBuilder();
|
187 |
213 |
try {
|
... | ... | |
199 |
225 |
}
|
200 |
226 |
|
201 |
227 |
public static PrintWriter getWriter(File file) throws UnsupportedEncodingException, FileNotFoundException {
|
|
228 |
|
202 |
229 |
return getWriter(file, UTF8);
|
203 |
230 |
}
|
204 |
231 |
|
205 |
232 |
public static PrintWriter getWriter(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException {
|
|
233 |
|
206 |
234 |
return getWriter(file, encoding, false);
|
207 |
235 |
}
|
208 |
236 |
|
209 |
237 |
public static PrintWriter getWriter(File file, boolean append) throws UnsupportedEncodingException, FileNotFoundException {
|
|
238 |
|
210 |
239 |
return getWriter(file, UTF8, append);
|
211 |
240 |
}
|
212 |
241 |
|
213 |
242 |
public static PrintWriter getWriter(File file, String encoding, boolean append) throws UnsupportedEncodingException, FileNotFoundException {
|
|
243 |
|
|
244 |
if (encoding == null) {
|
|
245 |
encoding = UTF8; // prevent null exception
|
|
246 |
}
|
|
247 |
|
214 |
248 |
return new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file, append)), encoding));
|
215 |
249 |
}
|
216 |
250 |
|
217 |
251 |
public static PrintWriter getWriter(String file) throws UnsupportedEncodingException, FileNotFoundException {
|
|
252 |
|
218 |
253 |
return getWriter(new File(file), UTF8);
|
219 |
254 |
}
|
220 |
255 |
|
221 |
256 |
public static boolean write(File file, String str) {
|
|
257 |
|
222 |
258 |
PrintWriter writer = null;
|
223 |
259 |
try {
|
224 |
260 |
writer = getWriter(file);
|
... | ... | |
235 |
271 |
}
|
236 |
272 |
|
237 |
273 |
public static ArrayList<String> getLines(File file, String encoding) {
|
|
274 |
|
238 |
275 |
ArrayList<String> lines = new ArrayList<String>();
|
239 |
276 |
try {
|
240 |
277 |
BufferedReader reader = IOUtils.getReader(file, encoding);
|
... | ... | |
258 |
295 |
* @throws IOException
|
259 |
296 |
*/
|
260 |
297 |
public static InputStream getInput(File file) throws MalformedURLException, IOException {
|
|
298 |
|
261 |
299 |
return file.toURI().toURL().openStream();
|
262 |
300 |
}
|
263 |
301 |
|
264 |
302 |
public static OutputStream getOutputStream(File file) throws FileNotFoundException {
|
|
303 |
|
265 |
304 |
return new BufferedOutputStream(new FileOutputStream(file, false));
|
266 |
305 |
}
|
267 |
306 |
|
268 |
307 |
public static OutputStream getOutputStream(File file, boolean append) throws FileNotFoundException {
|
|
308 |
|
269 |
309 |
return new BufferedOutputStream(new FileOutputStream(file, append));
|
270 |
310 |
}
|
271 |
311 |
|
272 |
312 |
public static void replace(File file, String oldString, String newString) throws IOException {
|
|
313 |
|
273 |
314 |
String text = getText(file, UTF8);
|
274 |
315 |
text = text.replace(oldString, newString);
|
275 |
316 |
IOUtils.write(file, text);
|
276 |
317 |
}
|
277 |
318 |
|
278 |
319 |
public static void replaceAll(File file, Pattern regex, String newString) throws IOException {
|
|
320 |
|
279 |
321 |
String text = getText(file, UTF8);
|
280 |
322 |
text = regex.matcher(text).replaceAll(newString);
|
281 |
323 |
IOUtils.write(file, text);
|