1 |
1 |
package org.txm.utils;
|
2 |
2 |
|
3 |
3 |
import java.io.File;
|
|
4 |
import java.io.FileFilter;
|
|
5 |
import java.io.FilenameFilter;
|
|
6 |
import java.util.ArrayList;
|
|
7 |
import java.util.Arrays;
|
|
8 |
import java.util.List;
|
|
9 |
import java.util.regex.Pattern;
|
4 |
10 |
|
5 |
11 |
import org.apache.commons.io.FilenameUtils;
|
6 |
12 |
|
... | ... | |
37 |
43 |
public static String stripExtension(File f) {
|
38 |
44 |
return FilenameUtils.getBaseName(f.getName());
|
39 |
45 |
}
|
|
46 |
|
|
47 |
/**
|
|
48 |
* select file and directories
|
|
49 |
* ignore hidden files
|
|
50 |
*
|
|
51 |
* @param file a directory or a file (its parent directory will be used)
|
|
52 |
* @return list of files, null if the file does not exists
|
|
53 |
*/
|
|
54 |
public File[] listFiles(File file) {
|
|
55 |
return listFiles(file, null, false, true);
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* select only files not hidden
|
|
60 |
*/
|
|
61 |
public static final FileFilter HIDDENFILE_FILTER = new FileFilter() {
|
|
62 |
@Override
|
|
63 |
public boolean accept(File file) {
|
|
64 |
return !file.isHidden() && !file.getName().startsWith(".") && !file.getName().endsWith("~");
|
|
65 |
}
|
|
66 |
};
|
40 |
67 |
|
|
68 |
/**
|
|
69 |
*
|
|
70 |
* @return a list containing the files not hidden
|
|
71 |
*/
|
|
72 |
public List<File> listFilesAsList(File dir) {
|
|
73 |
|
|
74 |
File[] files = dir.listFiles(HIDDENFILE_FILTER);
|
|
75 |
if (files == null)
|
|
76 |
return new ArrayList<File>();
|
|
77 |
|
|
78 |
return Arrays.asList(files);
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
*
|
|
83 |
* @param file a directory or a file (its parent directory will be used)
|
|
84 |
* @param namePattern null or filename regex
|
|
85 |
* @return list of files, null if the file does not exists
|
|
86 |
*/
|
|
87 |
public File[] listFiles(File file, String namePattern, boolean fileOnly, boolean ignoreHiddenFiles) {
|
|
88 |
|
|
89 |
if (!file.isDirectory()) {
|
|
90 |
file = file.getParentFile();
|
|
91 |
}
|
|
92 |
if (file == null) {
|
|
93 |
return null;
|
|
94 |
}
|
|
95 |
|
|
96 |
File[] files = file.listFiles(new PatternFilter(namePattern, fileOnly, ignoreHiddenFiles));
|
|
97 |
|
|
98 |
if (files != null) {
|
|
99 |
Arrays.sort(files); // always sort files by name
|
|
100 |
}
|
|
101 |
|
|
102 |
return files;
|
|
103 |
}
|
|
104 |
|
|
105 |
public static class PatternFilter implements FileFilter {
|
|
106 |
|
|
107 |
public Pattern mNamePattern = null;
|
|
108 |
boolean fileOnly;
|
|
109 |
boolean ignoreHiddenFiles;
|
|
110 |
|
|
111 |
public PatternFilter(String pattern, boolean fileOnly, boolean ignoreHiddenFiles) {
|
|
112 |
if (pattern != null && pattern.length() > 0) {
|
|
113 |
mNamePattern = Pattern.compile(pattern);
|
|
114 |
}
|
|
115 |
this.ignoreHiddenFiles = ignoreHiddenFiles;
|
|
116 |
this.fileOnly = fileOnly;
|
|
117 |
}
|
|
118 |
|
|
119 |
@Override
|
|
120 |
public boolean accept(File file) {
|
|
121 |
|
|
122 |
if (fileOnly && !file.isFile()) return false;
|
|
123 |
|
|
124 |
if (ignoreHiddenFiles && !HIDDENFILE_FILTER.accept(file)) return false;
|
|
125 |
|
|
126 |
if (mNamePattern == null) return true; // no more tests
|
|
127 |
|
|
128 |
return mNamePattern.matcher(file.getName()).matches(); // test the whole filename
|
|
129 |
}
|
|
130 |
|
|
131 |
}
|
|
132 |
|
41 |
133 |
public static void main(String[] args) {
|
42 |
134 |
File f = new File("test.gz.Txt");
|
43 |
135 |
System.out.println("base=" + stripExtension(f));
|