How to sort files in a directory in java

Posted on April 27, 2017


The following examples demonstrate how to sort files and directories in a directory by name, type, size and last modified date.

Sorting files and directories by name

The following example sorts files and directories in a directory by their name in alphabetical order.

FileSortExample1.java

package com.boraji.tutorial.file;

import java.io.File;
import java.util.Arrays;

public class FileSortExample1 {
   public static void main(String[] args) {
      File dir = new File("C:/Program Files/Java/jdk1.8.0_71");

      File[] files = dir.listFiles();

      Arrays.sort(files, (f1, f2) -> f1.compareTo(f2));

      for (File file : files) {
         if (!file.isHidden()) {
            if (file.isDirectory()) {
               System.out.println("DIR \t" + file.getName());
            } else {
               System.out.println("FILE\t" + file.getName());
            }
         }
      }
   }
}

Output

DIR 	bin
FILE	COPYRIGHT
DIR 	db
DIR 	include
FILE	javafx-src.zip
DIR 	jre
DIR 	lib
FILE	LICENSE
FILE	README.html
FILE	release
FILE	src.zip
FILE	THIRDPARTYLICENSEREADME-JAVAFX.txt
FILE	THIRDPARTYLICENSEREADME.txt

Sorting files and directories by name and type

The following example sorts files and directories in a directory by their name and type ( i.e. File or Directory).

FileSortExample2.java

package com.boraji.tutorial.file;

import java.io.File;
import java.util.Arrays;

public class FileSortExample2 {
   public static void main(String[] args) {
      File dir = new File("C:/Program Files/Java/jdk1.8.0_71");

      File[] files = dir.listFiles();

      Arrays.sort(files, (f1, f2) -> {
         if (f1.isDirectory() && !f2.isDirectory()) {
            return -1;
         } else if (!f1.isDirectory() && f2.isDirectory()) {
            return 1;
         } else {
            return f1.compareTo(f2);
         }
      });

      for (File file : files) {
         if (!file.isHidden()) {
            if (file.isDirectory()) {
               System.out.println("DIR \t" + file.getName());
            } else {
               System.out.println("FILE\t" + file.getName());
            }
         }
      }
   }
}

Output

DIR 	bin
DIR 	db
DIR 	include
DIR 	jre
DIR 	lib
FILE	COPYRIGHT
FILE	javafx-src.zip
FILE	LICENSE
FILE	README.html
FILE	release
FILE	src.zip
FILE	THIRDPARTYLICENSEREADME-JAVAFX.txt
FILE	THIRDPARTYLICENSEREADME.txt

 

Sorting files by size

The following example sorts files in a directory by their size.

FileSortExample3.java

package com.boraji.tutorial.file;

import java.io.File;
import java.util.Arrays;

public class FileSortExample3 {
   public static void main(String[] args) {
      File dir = new File("C:/Program Files/Java/jdk1.8.0_71");

      File[] files = dir.listFiles();

      Arrays.sort(files, (f1, f2) -> {
         return new Long(f1.length()).compareTo(new Long(f2.length()));
      });

      for (File file : files) {
         if (!file.isHidden()) {
            if (!file.isDirectory()) {
               System.out.println("FILE\t" + " " + file.length() + " bytes\t\t" + file.getName());
            }
         }
      }
   }
}

Output

FILE	 40 bytes		LICENSE
FILE	 159 bytes		README.html
FILE	 527 bytes		release
FILE	 3244 bytes		COPYRIGHT
FILE	 110114 bytes		THIRDPARTYLICENSEREADME-JAVAFX.txt
FILE	 177094 bytes		THIRDPARTYLICENSEREADME.txt
FILE	 5104612 bytes		javafx-src.zip
FILE	 21245394 bytes		src.zip

Sorting files and directories by last modified date

FileSortExample4.java

package com.boraji.tutorial.file;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class FileSortExample4 {
   public static void main(String[] args) {
      File dir = new File("C:/Program Files/Java/jdk1.8.0_71");

      File[] files = dir.listFiles();

      Arrays.sort(files, (f1, f2) -> {
         return new Date(f1.lastModified()).compareTo(new Date(f2.lastModified()));
      });

      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

      for (File file : files) {
         if (!file.isHidden()) {
            if (file.isDirectory()) {
               System.out.println("DIR \t" + " " 
                     + df.format(new Date(file.lastModified())) + "\t" + file.getName());
            } else {
               System.out.println("FILE\t" + " " 
                     + df.format(new Date(file.lastModified())) + "\t" + file.getName());
            }
         }
      }
   }
}

Output

FILE	 2015-12-22 07:01:18	COPYRIGHT
FILE	 2015-12-22 07:01:22	src.zip
FILE	 2016-02-07 10:22:36	LICENSE
FILE	 2016-02-07 10:22:36	README.html
FILE	 2016-02-07 10:22:36	THIRDPARTYLICENSEREADME-JAVAFX.txt
FILE	 2016-02-07 10:22:37	THIRDPARTYLICENSEREADME.txt
DIR 	 2016-02-07 10:22:37	bin
DIR 	 2016-02-07 10:22:37	db
DIR 	 2016-02-07 10:22:37	include
FILE	 2016-02-07 10:22:37	javafx-src.zip
DIR 	 2016-02-07 10:22:37	jre
FILE	 2016-02-07 10:22:47	release
DIR 	 2016-02-07 10:22:49	lib