Apache Commons IO Tutorial
Selected Reading
- Apache Commons IO - Discussion
- Apache Commons IO - Useful Resources
- Apache Commons IO - Quick Guide
- Apache Commons IO - TeeOutputStream
- Apache Commons IO - TeeInputStream
- LastModifiedFileComparator
- Apache Commons IO - SizeFileComparator
- Apache Commons IO - NameFileComparator
- Apache Commons IO - FileAlterationMonitor
- Apache Commons IO - FileAlterationObserver
- Apache Commons IO - FileEntry
- Apache Commons IO - AndFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - NameFileFilter
- Apache Commons IO - LineIterator
- Apache Commons IO - IOCase
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - IOUtils
- Apache Commons IO - Environment Setup
- Apache Commons IO - Overview
- Apache Commons IO - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache Commons IO - SizeFileComparator
Apache Commons IO - SizeFileComparator
SizeFileComparator compare the sizes of two files/directory. It can be used to sort the psts or arrays of files using their size or directories, based on their number of children.
Class Declaration
Following is the declaration for
org.apache.commons.io.comparator.SizeFileComparator Class −
pubpc class SizeFileComparator extends Object implements Seriapzable
Example of SizeFileComparator Class
Here is the input file we need to parse −
Welcome to TutorialsPoint. Simply Easy Learning.
IOTester.java
import java.io.File; import java.io.FileFilter; import java.io.IOException; import org.apache.commons.io.comparator.SizeFileComparator; import org.apache.commons.io.filefilter.FileFileFilter; pubpc class IOTester { pubpc static void main(String[] args) { try { usingSizeFileComparator(); } catch(IOException e) { System.out.println(e.getMessage()); } } pubpc static void usingSizeFileComparator() throws IOException { //get the current directory File currentDirectory = new File("."); SizeFileComparator comparator = new SizeFileComparator(); File[] sortedFiles = comparator.sort(currentDirectory.pstFiles((FileFilter)FileFileFilter.FILE)); System.out.println("Sorted By Size: "); for(File file:sortedFiles) { System.out.println(file.getName() + ", size(kb) :" + file.length()); } } }
Output
It will print the following result.
Sorted By Size: input.txt, size:124 .project, size:382 .classpath, size:441Advertisements