Child: [r23] (diff)

Download this file

files.java    473 lines (404 with data), 13.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Methods to ease the handling of files
*/
package utils;
import java.io.*;
import java.util.ArrayList;
/**
*
* @author Nuno Brito, 6th of June 2011 in Darmstadt, Germany.
*/
public class files {
/** Writes an inputstream back into a file */
static public void inputFileStreamToFile(InputStream inputStream,
File outFile){
OutputStream out = null;
try{
out = new FileOutputStream(outFile);
byte buf[]=new byte[1024];
int len;
while((len=inputStream.read(buf))>0)
out.write(buf,0,len);
inputStream.close();
}
catch (IOException e){}
finally {
try {
out.close();
} catch (IOException ex) {
}
}
}
/** Returns the extension of a file*/
public static String getExtension(File file){
String result;
String filename = file.getName();
// no dot found? no need to proceed
if(filename.contains(".")==false){
return "";
}
int mid= filename.lastIndexOf(".");
result = filename.substring(mid+1, filename.length());
// ensure that we always get things in lowercase
result = result.toLowerCase();
return result;
}
public static String readAsString(File file){
long length = file.length();
byte[] bytes = new byte[(int) length];
InputStream is = null;
try{
is = new FileInputStream(file);
is.read(bytes);
}catch(IOException e){}
finally{
try {
if(is!=null){
is.close();
}
} catch (IOException ex) {
}
}
return new String(bytes);
}
// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
/**
* Counts the size of all files inside a given folder
* @param where The folder where we want to count the files
* @return
*/
public static long folderSize(File where){
ArrayList<File> folders = findFiles(where);
long counter = 0;
for(File folder : folders){
counter += folder.length();
}
return counter;
}
/**
* Find all files in a given folder and respective subfolders
* @param where A file object of the start folder
* @param maxDeep How deep is the crawl allowed to proceed
* @return An array containing all the found files, returns null if none is
* found
*/
public static ArrayList<File> findFiles(File where){
return findFiles(where, 25);
}
/**
* Find all files in a given folder and respective subfolders
* @param where A file object of the start folder
* @param maxDeep How deep is the crawl allowed to proceed
* @return An array containing all the found files, returns null if none is
* found
*/
public static ArrayList<File> findFilesFiltered(File where, final String what, int maxDeep){
File[] files = where.listFiles();
ArrayList<File> result = new ArrayList<File>();
if(files != null)
for (File file : files) {
if (file.isFile()){
if(file.getName().contains(what))
result.add(file);}
else
if ( (file.isDirectory())
&&( maxDeep-1 > 0 ) ){
// do the recursive crawling
ArrayList<File> temp = findFilesFiltered(file, what, maxDeep-1);
for(File thisFile : temp)
result.add(thisFile);
}
}
return result;
}
/**
* Find all files in a given folder and respective subfolders
* @param where A file object of the start folder
* @param maxDeep How deep is the crawl allowed to proceed
* @return An array containing all the found files, returns null if none is
* found
*/
public static ArrayList<File> findFiles(File where, int maxDeep){
File[] files = where.listFiles();
ArrayList<File> result = new ArrayList<File>();
if(files != null)
for (File file : files) {
if (file.isFile())
result.add(file);
else
if ( (file.isDirectory())
&&( maxDeep-1 > 0 ) ){
// do the recursive crawling
ArrayList<File> temp = findFiles(file, maxDeep-1);
for(File thisFile : temp)
result.add(thisFile);
}
}
return result;
}
/** Get the size of a given folder and respective files inside subfolders */
public static long getFolderSize(File where, int maxDeep){
// output variable
long result = 0;
// get all files from the given folder
ArrayList<File> files = findFiles(where, 25);
// iterate each file and sum the value
for(File file : files)
result += file.length();
// output our result
return result;
}
/**
* Simpler version of find all folders on a given location
* @param where
* @return
*/
public static ArrayList<File> findFolders(File where){
return findFolders(where, 25);
}
/**
* Find all subfolders in a given folder.
* @param where A file object of the start folder
* @param maxDeep How deep is the crawl allowed to proceed
* @return An array containing all the found files, returns null if none is
* found
*/
public static ArrayList<File> findFolders(File where, int maxDeep){
File[] files = where.listFiles();
ArrayList<File> result = new ArrayList<File>();
if(files != null)
for (File file : files) {
if ( (file.isDirectory())
&&( maxDeep-1 > 0 ) ){
result.add(file);
// do the recursive crawling
ArrayList<File> temp = findFolders(file, maxDeep-1);
for(File thisFile : temp)
result.add(thisFile);
}
}
return result;
}
/**
* Find all files and subfolders inside a given folder.
* @param where A file object of the start folder
* @param maxDeep How deep is the crawl allowed to proceed
* @return An array containing all the found files, returns null if none is
* found
*/
public static ArrayList<File> findAll(File where, int maxDeep){
File[] files = where.listFiles();
ArrayList<File> result = new ArrayList<File>();
if(files != null)
for (File file : files) {
if (file.isFile())
result.add(file);
else
if ( (file.isDirectory())
&&( maxDeep-1 > 0 ) ){
result.add(file);
// do the recursive crawling
ArrayList<File> temp = findAll(file, maxDeep-1);
for(File thisFile : temp)
result.add(thisFile);
}
}
return result;
}
/**
* Find all subfolders inside a given folder.
* @param where A file object of the start folder
* @return An array containing all the found files, returns null if none is
* found
*/
public static ArrayList<File> findSubFolders(File where){
// 2013-MAY-11 psc added
File[] files = where.listFiles();
ArrayList<File> result = new ArrayList<File>();
if(files != null)
for (File file : files) {
if (file.isDirectory()) {
result.add(file);
}
}
return result;
}
/**
* Ensures that we can pick on a value and present a readable
* size of the file instead of plain bytes
*
* @param size
* @return String
*/
public static String humanReadableSize(Long size){
long l = size;
//Long.parseLong(size.trim());
String output;
long b;
long MEGABYTE = 1024L * 1024L;
long KILOBYTE = 1024;
if (l > MEGABYTE){
b = l / MEGABYTE;
output = Long.toString(b)+" Mb";}
else
if (l > KILOBYTE){
b = l / KILOBYTE;
output = Long.toString(b)+" Kb";}
else
output = size+" bytes";
return output;
}
/** get the folder where our user documents are placed */
public static synchronized String getDocumentsDirectory(){
String result = "";
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
javax.swing.JFileChooser jFileChooser1 = new javax.swing.JFileChooser();
String myresult = jFileChooser1.getFileSystemView().getDefaultDirectory()
.getAbsolutePath();
}
});
return result;
}
/** get the desktop folder */
public static synchronized String getDesktopDirectory(){
javax.swing.JFileChooser jFileChooser1 = new javax.swing.JFileChooser();
return jFileChooser1.getFileSystemView().getHomeDirectory()
.getAbsolutePath();
}
/** get our home folder (under windows this value is not certain)*/
public static synchronized String getHomeDirectory(){
javax.swing.JFileChooser jFileChooser1
= new javax.swing.JFileChooser();
return jFileChooser1.getFileSystemView().getParentDirectory(
jFileChooser1.getFileSystemView().createFileObject
(getDesktopDirectory())
).getAbsolutePath();
}
/** create a folder along with respective parent folders if needed */
public static Boolean mkdirs(String folder){
boolean result;
File docs = new File(folder);
result = docs.mkdirs();
return result;
}
/** create a folder along with respective parent folders if needed */
public static Boolean mkdirs(File docs){
boolean result;
result = docs.mkdirs();
return result;
}
/**
* Touch a file, if it does not exist then create an empty file.
* @param file
* @return
*/
public static boolean touch(File folder, String file){
File touch = new File(folder, file);
try {
touch.createNewFile();
} catch (IOException ex) {
return false;
}
return true;
}
/**
* This method saves the contents from a string to a file
*
* @author Nuno Brito
* @version 1.0
* @date 2010/06/06
*/
public static boolean SaveStringToFile(File inputFile, String inputString){
try {
BufferedWriter out = new BufferedWriter(new FileWriter(inputFile));
out.write(inputString);
out.close();
}
catch (IOException e){
System.out.println(e.getMessage());
return false;
}
return true;
}
/**
* Load a text file contents as a <code>String<code>.
* This method does not perform enconding conversions
*
* @param file The input file
* @return The file contents as a <code>String</code>
* @exception IOException IO Error
*/
public static String deserializeString(File file)
// code copied from http://goo.gl/5qa3y
throws IOException {
int len;
char[] chr = new char[4096];
final StringBuffer buffer = new StringBuffer();
final FileReader reader = new FileReader(file);
try {
while ((len = reader.read(chr)) > 0) {
buffer.append(chr, 0, len);
}
} finally {
reader.close();
}
return buffer.toString();
}
// /**
// * Copies a file from one location to another
// * @param FromFile Source file
// * @param ToFile Place where we want the new file to be copied
// * @return
// */
// static public Boolean copyFile(File FromFile, File ToFile){
// Boolean result = false;
//
// // do a quick test, if the file is already there then don't overwrite
// if(ToFile.exists() && (FromFile.length() == ToFile.length())){
// // speed up our proceess.
// return true;
// }
//
// try {
// // using Apache commons IO for added performance
// FileUtils.copyFile(FromFile, ToFile, true);
// result = ToFile.exists();
// } catch (IOException ex) {
// }
// return result;
// }
//
// /**
// * Copies a file from one location to another
// * @param FromFile Source file
// * @param ToFile Place where we want the new file to be copied
// * @return
// */
// static public Boolean copyFolder(File FromFolder, File ToFolder){
// Boolean result = true;
// try {
// // using Apache commons IO for added performance
// FileUtils.copyDirectory(FromFolder, ToFolder, true);
// } catch (IOException ex) {
// result = false;
// }
// return result;
// }
}