English 中文(简体)
Apache Commons IO - NameFileComparator
  • 时间:2024-09-17

Apache Commons IO - NameFileComparator


Previous Page Next Page  

NameFileComparator compare the names of two files. It can be used to sort the psts or arrays of files, using their name, either in a case-sensitive, case-insensitive or system dependent case sensitive way.

Class Declaration

Following is the declaration for

org.apache.commons.io.comparator.NameFileComparator Class −


pubpc class NameFileComparator
   extends Object implements Seriapzable

Example of NameFileComparator 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.IOCase;
import org.apache.commons.io.comparator.NameFileComparator;
import org.apache.commons.io.filefilter.FileFileFilter;
pubpc class IOTester {
   pubpc static void main(String[] args) {
      try {
         usingNameFileComparator();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   pubpc static void usingNameFileComparator() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      NameFileComparator comparator = new
      NameFileComparator(IOCase.INSENSITIVE);
      File[] sortedFiles = comparator.sort(currentDirectory.pstFiles((FileFilter)FileFileFilter.FILE));
      System.out.println("Sorted By Name: ");
      for(File file:sortedFiles) {
         System.out.println(file.getName());
      }
   }
}

Output

It will print the following result.


Sorted By Name:
.classpath
.project
input.txt
Advertisements