Download this file

FileTools.java    409 lines (382 with data), 13.3 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
package cologne.eck.tools;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import cologne.eck.all_peas.data.PeaProperties;
public class FileTools {
/**
* Get all files of a directory including sub-directories
* as an unsorted list.
*
* @param directory the directory
* @param filesOnly true if only file should be included
* false if files and directories should be included
*
* @return an unsorted list of all included files
* or null if the directory is null
* or is not to a directory
*/
public static ArrayList <File> listAllFilesOfFolder(File directory, boolean filesOnly) {
if (directory == null) {
System.err.println("FileTools: directory is null");
return null;
}
if (! directory.isDirectory()) {
System.err.println("FileTools: argument is not directory");
return null;
}
ArrayList<File> resultList = new ArrayList<File>();
File[] fileList = directory.listFiles();
if (fileList == null) {
System.err.println("FileTools: No files in directory");
return null;
}
int len = fileList.length;
for (int i = 0; i < len; i++) {
File file = fileList[i];
//resultList.add(file);
if (file.isFile()) {
resultList.add(file);
//System.out.println(file.getAbsolutePath());
} else if (file.isDirectory()) {
if (filesOnly == false){
resultList.add(file);
}
if (listAllFilesOfFolder(file, filesOnly) != null){
resultList.addAll(listAllFilesOfFolder(file, filesOnly));
}
}
}
return resultList;
}
/**
* Get all names of files of a directory including sub-directories
* as an unsorted list.
*
* @param directoryName the name of the directory
* @param filesOnly true if only file should be included
* false if files and directories should be included
*
* @return an unsorted list of all included file names
* or null if the directory name is null
* or refers not to a directory
*/
public static ArrayList <String> listAllFileNamesOfFolder(String directoryName, boolean filesOnly) {
if (directoryName == null) {
System.err.println("FileTools: directory nam is null");
return null;
}
File directory = new File(directoryName);
if (! directory.isDirectory()) {
System.err.println("FileTools: argument is not directory");
return null;
}
ArrayList<String> resultList = new ArrayList<String>();
File[] fileList = directory.listFiles();
int len = fileList.length;
for (int i = 0; i < len; i++) {
File file = fileList[i];
resultList.add(file.getAbsolutePath());
if (file.isFile()) {
resultList.add(file.getAbsolutePath());
//System.out.println(file.getAbsolutePath());
} else if (file.isDirectory()) {
if (filesOnly == false){
resultList.add(file.getAbsolutePath());
}
resultList.addAll(listAllFileNamesOfFolder(file.getAbsolutePath(), filesOnly));
}
}
return resultList;
}
/**
* Get all names of files of a directory including sub-directories
* as an array of Strings.
*
* @param directoryName the name of the directory
* @param filesOnly true if only file should be included
* false if files and directories should be included
*
* @return an array of all included file names
* or null if the directory name is null
* or refers not to a directory
*/
public static String[] getAllFileNamesOfFolder(String directoryName, boolean filesOnly) {
ArrayList <String> list = listAllFileNamesOfFolder(directoryName, filesOnly);
if (list == null) {
return null;
} else {
String[] array = new String[list.size()];
return list.toArray(array);
}
}
/**
* Get all files of a directory including sub-directories
* as an array of Strings.
*
* @param directoryName the directory
* @param filesOnly true if only file should be included
* false if files and directories should be included
*
* @return an array of all included files
* or null if the directory name is null
* or refers not to a directory
*/
public static File[] getAllFilesOfFolder(File directory, boolean filesOnly) {
ArrayList <File> list = listAllFilesOfFolder(directory, filesOnly);
if (list == null) {
return null;
} else {
File[] array = new File[list.size()];
//System.out.println("list size: " + array.length);
return list.toArray(array);
}
}
/**
* Get the file size in bytes
*
* @param fileName the name of the file
* @return the number of bytes (long value)
*/
public final static long getFileSize(File file) {
RandomAccessFile raf = null;
if (file.isDirectory() ) {
return 0;
}
long result = 0;
try {
raf = new RandomAccessFile(file, "r");
result = raf.length();
raf.close();
} catch (FileNotFoundException e) {
// this will be thrown if file is not file and not directory
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + file.getAbsolutePath());
}
} catch (IOException e) {
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + file.getAbsolutePath());
}
} catch (Exception e) {
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + file.getAbsolutePath());
}
}
return result;
}
/**
* Get the file size in bytes
*
* @param fileName the name of the file
* @return the number of bytes (long value)
*/
public final static long getFileSize(String fileName) {
RandomAccessFile raf = null;
File file = new File(fileName);
if (file.isDirectory() ) {
return 0;
}
long result = 0;
try {
raf = new RandomAccessFile(file, "r");
result = raf.length();
raf.close();
} catch (FileNotFoundException e) {
// this will be thrown if file is not file and not directory
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + fileName);
}
} catch (IOException e) {
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + fileName);
}
} catch (Exception e) {
if ( PeaProperties.getWorkingMode().equals("-t")) {
System.err.println(e.toString() + ", " + e.getMessage() + ": " + fileName);
}
}
return result;
}
/**
* Get the number and overall size of files.
* There is no warning for long execution time!
*
* @param fileNames an array of file names
* @return an array of long values: the first value [0] is the
* number of the containing files (no directories),
* the second [1] is the size of all included files.
* There is no warning for long execution time!
*/
public static final long[] getNumberAndSize(String[] fileNames){
long[] result = new long[2];
if (fileNames == null){
System.err.println("FileTools getNumberAndSize[]: fileNames null");
return null;
}
int len = fileNames.length;
if (len == 0) {
result[0] = 0;
result[1] = 0;
return result;
}
RandomAccessFile raf = null;
for (int i = 0; i < len; i++) {
if (! new File(fileNames[i]).isFile()){
// do not count directories and other
continue;
}
try {
raf = new RandomAccessFile(fileNames[i], "r");
result[1] += raf.length();
raf.close();
} catch (FileNotFoundException e) {
System.err.println(e.toString() +" FileTools getNumberAndSize[]: " + e.getMessage() + ", " + fileNames[i]);
} catch (IOException e) {
//read_write_failed = true;
System.err.println(e.toString() + " FileTools getNumberAndSize[]: " + e.getMessage() + ", " + fileNames[i]);
//e.printStackTrace();
} catch (Exception e) {
System.err.println(e.toString() + " - " + e.getMessage() + ", " + fileNames[i]);
//e.printStackTrace();
}
// only count files, not folders:
result[0] ++;
}
return result;
}
/**
* Get the number of files of a directory - inclusive
* all files of sub-directories, but no directory itself.
* There is no warning for long execution time!
*
* @param directoryName the name of the directory to check
* @return an array of long values: the first value [0] is the
* number of the containing files (no directories),
* the second [1] is the size of all included files.
* There is no warning for long execution time!
*/
public static final long[] getNumberAndSize(String directoryName) {
File directory = new File(directoryName);
RandomAccessFile raf = null;
long[] result = new long[2];
File[] children = directory.listFiles();
if (children != null) {
for (int i = 0; i < children.length; i++) {
if (children[i].isDirectory() ) {
long[] dirResult = getNumberAndSize(children[i].getAbsolutePath());
// function was broken because of extremeSizeWarning
// this avoids NullPointerException
if (dirResult == null) {
return null;
}
result[0] += dirResult[0];
result[1] += dirResult[1];
} else if (children[i].isFile() ) {
try {
raf = new RandomAccessFile(children[i], "r");
result[1] += raf.length();
raf.close();
} catch (FileNotFoundException e) {
//file_not_found = true;
System.err.println("FileTools getNumberAndSize: " + e.getMessage() + ", " + children[i]);
//e.printStackTrace();
} catch (IOException e) {
//read_write_failed = true;
System.err.println("FileTools getNumberAndSize: " + e.getMessage() + ", " + children[i]);
//e.printStackTrace();
} catch (Exception e) {
System.err.println(e.toString() + " - " + e.getMessage() + ", " + children[i]);
//e.printStackTrace();
}
// only count files, not folders:
result[0] ++;
}
}
}
return result;
}
/**
* Checks a file if it is a valid to encrypt/decrypt or not.
* Checks existence, read and write access, if empty or too large.
* If the file is invalid, the function return one of these five
* error string:
* can_not_find, can_not_read, can_not_write, empty_file, file_too_large.
* If the file is valid, the functions returns null.
*
* @param file the file to check
* @param printErrors print error messages to stderr (true) or not (false)
*
* @return an error string if the file is invalid
* or null if the file is valid
*/
public final static String checkAccess(File file, boolean printErrors) {
if ( ! file.exists() ) {
if (printErrors == true) {
System.err.println("File does not exist: " + file.getAbsolutePath());
}
return "can_not_find";
}
if ( ! file.canRead() ) {
if (printErrors == true) {
System.err.println("No read access to file: " + file.getAbsolutePath());
}
return "can_not_read";
}
if ( ! file.canWrite() ) {
if (printErrors == true) {
System.err.println("No write access to file: " + file.getAbsolutePath());
}
return "can_not_write";
}
if ( file.length() == 0 && ! file.isDirectory() ) {
if (printErrors == true) {
System.err.println("Empty file: " + file.getAbsolutePath());
}
return "empty_file";
}
if ( file.length() > Long.MAX_VALUE || file.length() < 0) {
if (printErrors == true) {
System.err.println("Invalid file size: " + file.getAbsolutePath());
}
return "file_too_large";
}
return null;
}
/**
* Get all parent directory names of a file as list.
* The direct directory is the first element of the
* list, the root directory the last element.
*
* @param file the file
*
* @return the list of all parent directory names, where
* the direct parent directory is the first element
*/
/* public static ArrayList<String> listParentDirectoryNames(File file) {
!!! untested !!!
ArrayList<String> list = new ArrayList<String>();
String parentName = file.getParent();
if (parentName == null) {
return null;
}
File parent = new File(parentName);
while (parent.isDirectory()) {
list.add(parentName);
parentName = parent.getParent();
if (parentName == null ){
break;
}
parent = new File(parentName);
if (parent.exists() == false) {
break;
}
}
if (list.isEmpty()){
return null;
} else {
return list;
}
} */
}