1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys;
17
18 import java.io.File;
19 import java.io.FilenameFilter;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.TreeSet;
25
26 import org.apache.commons.io.FileUtils;
27
28
29
30
31 public class FileUtil {
32 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FileUtil.class);
33
34 private static Set<String> createdDirectory = new TreeSet<String>();
35
36
37
38
39
40
41
42 public static File getNewestFile(File directory, FilenameFilter filenameFilter) {
43 File newestFile = null;
44
45 File[] directoryListing = directory.listFiles(filenameFilter);
46 if (directoryListing == null || directoryListing.length == 0) {
47 return null;
48 } else {
49 for (int i = 0; i < directoryListing.length; i++) {
50 File file = directoryListing[i];
51 if (newestFile == null) {
52 newestFile = file;
53 } else {
54 if (newestFile.lastModified() < file.lastModified()){
55 newestFile = file;
56 }
57 }
58 }
59 }
60
61 return newestFile;
62 }
63
64
65
66
67
68
69
70 public static void createDirectory(final String directoryPath) {
71 createDirectories(new ArrayList<String>(){{add(directoryPath);}});
72 }
73
74
75
76
77
78
79 public static void createDirectories(List<String> directoryPathList) {
80 File directoryToCheck;
81 for (String path : directoryPathList){
82 if (path != null) {
83 directoryToCheck = new File(path);
84 if (!directoryToCheck.isDirectory() && !createdDirectory.contains(path)) {
85 try {
86 FileUtils.forceMkdir(directoryToCheck);
87 LOG.debug("[" + path + "] has been created successfully");
88
89
90 createdDirectory.add(path);
91 }
92 catch (IOException ex) {
93 LOG.warn("Unable to create directory [" + path + "]", ex);
94 }
95 }
96 }
97 }
98 }
99 }