How do I sort an array of files according to their sizes?
Author: Deron Eriksson
Description: This Java tutorial describes how to sort an array of files according to their sizes using a Comparator from Commons IO.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
The SizeFileComparator class in the ApacheSW Commons IOS library has several Comparator singletons for sorting files according to file size. Files can be sorted in ascending and descending size order. SIZE_COMPARATOR and SIZE_REVERSE treat directory sizes as 0, while SIZE_SUMDIR_COMPARATOR and SIZE_SUMDIR_REVERSE treat directory sizes as the sum of their contents. The SizeFileComparatorTest class demonstrates this. SizeFileComparatorTest.javapackage test; import java.io.File; import java.io.IOException; import java.util.Arrays; import org.apache.commons.io.FileUtils; import org.apache.commons.io.comparator.SizeFileComparator; public class SizeFileComparatorTest { public static void main(String[] args) throws IOException { File directory = new File("."); File[] files = directory.listFiles(); System.out.println("Default order"); displayFiles(files); Arrays.sort(files, SizeFileComparator.SIZE_COMPARATOR); System.out.println("\nSizeFileComparator.SIZE_COMPARATOR (Ascending, directories treated as 0)"); displayFiles(files); Arrays.sort(files, SizeFileComparator.SIZE_REVERSE); System.out.println("\nSizeFileComparator.SIZE_REVERSE (Descending, directories treated as 0)"); displayFiles(files); Arrays.sort(files, SizeFileComparator.SIZE_SUMDIR_COMPARATOR); System.out.println("\nSizeFileComparator.SIZE_SUMDIR_COMPARATOR (Ascending, directory size used)"); displayFilesWithDirectorySizes(files); Arrays.sort(files, SizeFileComparator.SIZE_SUMDIR_REVERSE); System.out.println("\nSizeFileComparator.SIZE_SUMDIR_REVERSE (Descending, directory size used)"); displayFilesWithDirectorySizes(files); } public static void displayFiles(File[] files) { for (File file : files) { System.out.printf("%-20s Size:" + file.length() + "\n", file.getName()); } } public static void displayFilesWithDirectorySizes(File[] files) { for (File file : files) { if (file.isDirectory()) { System.out.printf("%-20s Size:" + FileUtils.sizeOfDirectory(file) + "\n", file.getName()); } else { System.out.printf("%-20s Size:" + file.length() + "\n", file.getName()); } } } } The console output from the execution of SizeFileComparatorTest is self-explanatory and shown below: ResultsDefault order .classpath Size:293 .project Size:383 bin Size:0 docs Size:0 lib Size:0 src Size:0 Test1.txt Size:17 test2.txt Size:19 Test3.txt Size:11 test4.txt Size:19 SizeFileComparator.SIZE_COMPARATOR (Ascending, directories treated as 0) bin Size:0 docs Size:0 lib Size:0 src Size:0 Test3.txt Size:11 Test1.txt Size:17 test2.txt Size:19 test4.txt Size:19 .classpath Size:293 .project Size:383 SizeFileComparator.SIZE_REVERSE (Descending, directories treated as 0) .project Size:383 .classpath Size:293 test2.txt Size:19 test4.txt Size:19 Test1.txt Size:17 Test3.txt Size:11 bin Size:0 docs Size:0 lib Size:0 src Size:0 SizeFileComparator.SIZE_SUMDIR_COMPARATOR (Ascending, directory size used) Test3.txt Size:11 Test1.txt Size:17 test2.txt Size:19 test4.txt Size:19 .classpath Size:293 .project Size:383 src Size:3513 bin Size:5417 lib Size:289835 docs Size:529577544 SizeFileComparator.SIZE_SUMDIR_REVERSE (Descending, directory size used) docs Size:529577544 lib Size:289835 bin Size:5417 src Size:3513 .project Size:383 .classpath Size:293 test2.txt Size:19 test4.txt Size:19 Test1.txt Size:17 Test3.txt Size:11 Related Tutorials:
|