private static final String PARENT_DIR =
"C:UsersLilykosworkspaceApacheCommonsExampleExampleFolder";
public static void runExample() {
System.out.println("File Filter example...");
// NameFileFilter
// Right now, in the parent directory we have 3 files:
// directory example
// file exampleEntry.txt
// file exampleTxt.txt
// Get all the files in the specified directory
// that are named "example".
File dir = FileUtils.getFile(PARENT_DIR);
String[] acceptedNames = {"example", "exampleTxt.txt"};
for (String file: dir.list(new NameFileFilter(acceptedNames, IOCase.INSENSITIVE))) {
System.out.println("File found, named: " + file);
}
//WildcardFileFilter
// We can use wildcards in order to get less specific results
// ? used for 1 missing char
// * used for multiple missing chars
for (String file: dir.list(new WildcardFileFilter("*ample*"))) {
System.out.println("Wildcard file found, named: " + file);
}
// PrefixFileFilter
// We can also use the equivalent of startsWith
// for filtering files.
for (String file: dir.list(new PrefixFileFilter("example"))) {
System.out.println("Prefix file found, named: " + file);
}
// SuffixFileFilter
// We can also use the equivalent of endsWith
// for filtering files.
for (String file: dir.list(new SuffixFileFilter(".txt"))) {
System.out.println("Suffix file found, named: " + file);
}
// OrFileFilter
// We can use some filters of filters.
// in this case, we use a filter to apply a logical
// or between our filters.
for (String file: dir.list(new OrFileFilter(
new WildcardFileFilter("*ample*"), new SuffixFileFilter(".txt")))) {
System.out.println("Or file found, named: " + file);
}
// And this can become very detailed.
// Eg, get all the files that have "ample" in their name
// but they are not text files (so they have no ".txt" extension.
for (String file: dir.list(new AndFileFilter( // we will match 2 filters...
new WildcardFileFilter("*ample*"), // ...the 1st is a wildcard...
new NotFileFilter(new SuffixFileFilter(".txt"))))) { // ...and the 2nd is NOT .txt.
System.out.println("And/Not file found, named: " + file);
}
}
}
private static final String PARENT_DIR =
"C:UsersLilykosworkspaceApacheCommonsExampleExampleFolder";
private static final String FILE_1 =
"C:UsersLilykosworkspaceApacheCommonsExampleExampleFolderexample";
private static final String FILE_2 =
"C:UsersLilykosworkspaceApacheCommonsExampleExampleFolderexampleTxt.txt";
public static void runExample() {
System.out.println("Comparator example...");
//NameFileComparator
// Let's get a directory as a File object
// and sort all its files.
File parentDir = FileUtils.getFile(PARENT_DIR);
NameFileComparator comparator = new NameFileComparator(IOCase.SENSITIVE);
File[] sortedFiles = comparator.sort(parentDir.listFiles());
System.out.println("Sorted by name files in parent directory: ");
for (File file: sortedFiles) {
System.out.println("t"+ file.getAbsolutePath());
}
//>
// We can compare files based on their>
// The boolean in the constructor is about the directories.
// true: directory's contents count to the>
// false: directory is considered zero>
SizeFileComparator>new SizeFileComparator(true);
File[]>
// We can use this>
LastModifiedFileComparator lastModified = new LastModifiedFileComparator();
File[] lastModifiedFiles = lastModified.sort(parentDir.listFiles());
System.out.println("Sorted by last modified files in parent directory: ");
for (File file: lastModifiedFiles) {
Date modified = new Date(file.lastModified());
System.out.println("t"+ file.getName() + " last modified on: " + modified);
}
// Or, we can also compare 2 specific files and find which one was last modified.
// returns > 0 if the first file was last modified.
// returns 0)
System.out.println("File " + file1.getName() + " was modified last because...");
else
System.out.println("File " + file2.getName() + "was modified last because...");
System.out.println("t"+ file1.getName() + " last modified on: " +
new Date(file1.lastModified()));
System.out.println("t"+ file2.getName() + " last modified on: " +
new Date(file2.lastModified()));
}
}
输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Comparator example...
Sorted by name files in parent directory:
C:UsersLilykosworkspaceApacheCommonsExampleExampleFoldercomparator1.txt
C:UsersLilykosworkspaceApacheCommonsExampleExampleFoldercomperator2.txt
C:UsersLilykosworkspaceApacheCommonsExampleExampleFolderexample
C:UsersLilykosworkspaceApacheCommonsExampleExampleFolderexampleFileEntry.txt
C:UsersLilykosworkspaceApacheCommonsExampleExampleFolderexampleTxt.txt
Sorted by>
example with>0
exampleTxt.txt with>87
exampleFileEntry.txt with>503
comperator2.txt with>1458
comparator1.txt with>4436
Sorted by last modified files in parent directory:
exampleTxt.txt last modified on: Sun Oct 26 14:02:22 EET 2014
example last modified on: Sun Oct 26 23:42:55 EET 2014
comparator1.txt last modified on: Tue Oct 28 14:48:28 EET 2014
comperator2.txt last modified on: Tue Oct 28 14:48:52 EET 2014
exampleFileEntry.txt last modified on: Tue Oct 28 14:53:50 EET 2014
File example was modified last because...
example last modified on: Sun Oct 26 23:42:55 EET 2014
exampleTxt.txt last modified on: Sun Oct 26 14:02:22 EET 2014 让我们来看看这里用到了哪些类:
NameFileComparator:通过文件名来比较文件。
SizeFileComparator:通过文件大小来比较文件。
LastModifiedFileComparator:通过文件的最新修改时间来比较文件。
在这里你需要注意,比较可以在定的文件夹中(文件夹下的文件已经被sort()方法排序过了),也可以在两个指定的文件之间(通过使用compare()方法)。
private static final String XML_PATH =
"C:UsersLilykosworkspaceApacheCommonsExampleInputOutputExampleFolderweb.xml";
private static final String INPUT = "This should go to the output.";
public static void runExample() {
System.out.println("Input example...");
XmlStreamReader xmlReader = null;
TeeInputStream tee = null;
try {
// XmlStreamReader
// We can read an xml file and get its encoding.
File xml = FileUtils.getFile(XML_PATH);
xmlReader = new XmlStreamReader(xml);
System.out.println("XML encoding: " + xmlReader.getEncoding());
// TeeInputStream
// This very useful>
// and closes both using only one close() method (by defining the 3rd
// constructor parameter as true).
ByteArrayInputStream in = new ByteArrayInputStream(INPUT.getBytes("US-ASCII"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
tee = new TeeInputStream(in, out, true);
tee.read(new byte[INPUT.length()]);
ByteArrayInputStream in = new ByteArrayInputStream(INPUT.getBytes("US-ASCII"));
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
ByteArrayOutputStream out2 = new ByteArrayOutputStream();
teeOut = new TeeOutputStream(out1, out2);
teeIn = new TeeInputStream(in, teeOut, true);
teeIn.read(new byte[INPUT.length()]);
} catch (IOException e) {
e.printStackTrace();
} finally {
// No need to close teeOut. When teeIn closes, it will also close its
// Output stream (which is teeOut), which will in turn close the 2
// branches (out1, out2).
try { teeIn.close(); }
catch (IOException e) { e.printStackTrace(); }
}
}
}
输出
1
2
3
Output example...
Output stream 1: This should go to the output.
Output stream 2: This should go to the output.