Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » Java » Programs » Sort files of any directory

Java program to sort files of any directory

This program has 3 files Main.java, SortType.java and MyFile.java
It asks to enter diretory name which can be in current directory and then ouputs list of files with their size.
It also allows user to sort the files using Seletion and Insertion sort

//Main.java file
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.io.File;

/**
 *
 * @author CppBuzz.com
 */
public class Main {
    Scanner sc = new Scanner(System.in);
    // contains a list of MyFile
    private List files = new ArrayList();
    
    public void loadFiles(String folder) {
    	File fd = new File(folder);
    	File listofFiles[] = fd.listFiles();
    
    	for (File f : listofFiles) {
    	MyFile mf = new MyFile(f.getName(), f.length(), f.getPath());
    	files.add(mf);
    	}
    }
    
    // list information of all loaded files
    public void list() {
    	if (files != null && files.size() > 0) {
    	System.out.println(String.format("%-20s%-10s", "Name", "Size(in byte)"));
    	for (MyFile f : files) {
    		System.out.println(f);
    	}
    	}
    }
    
    // sort the list of files ascending by size (use selection sort)
    public void selectionSort() {
    
    	for (int i = 0; i < files.size(); i++) {
    	for (int j = i + 1; j < files.size(); j++)
    	if (files.get(i).getSize() > files.get(j).getSize()) {
    		// System.out.println("==file1 size : " + files[i].getSize() + "==file2 size :"
    		// + files[j].getSize());
    		MyFile obj = files.get(i);
    		files.set(i, files.get(j));
    		files.set(j, obj);
    		}
    	}
    
    }
    
    // sort the list of files ascending by size (use insertion sort)
    public void insertionSort() {
    	int i, j;
    	for (i = 1; i < files.size(); i++) {
    	j = i - 1;
    	while (j >= 0 && files.get(j).getSize() > files.get(i).getSize()) {
    		MyFile obj = files.get(i);
    		files.set(i, files.get(j));
    		files.set(j, obj);
    		i = j;
    		j--;
    	}
    	}
    }
    
    // sort and output sorted list of text files
    public void sort(SortType st) {
    
    	if (st == SortType.INSERTTIONSORT) {
    		insertionSort();
    	} else if (st == SortType.SELECTIONSORT) {
    		selectionSort();
    	}
    
    }
    
    // return true if given MyFile contains given keyword, otherwise return false
    public boolean searchFile(MyFile mf, String keyword) {
    	//to be implemented
    	if (!mf.getName().toLowerCase().endsWith(".txt")) {
    		return false;
    	}
    	throw new UnsupportedOperationException("Remove this line and implement your code here!");
    }
    
    // output information of all files which content has given keyword
    public void searchFile(String keyword) {
    	// not implemented yet
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        File f = null;
        int tc;
        int tc2;
        
        String str2;
        Main X = new Main();
        System.out.println("Menu");
        System.out.println("1. Load files");
        System.out.println("2. Sort files");
        System.out.println("3. Search files");
        System.out.println("0. Exit");
        System.out.print("Enter your choice: ");
        tc = sc.nextInt();
        if (tc == 1) {
        	System.out.print("Enter a folder : ");
        	str2 = sc.next();
        	f = new File(str2);
        	X.loadFiles(str2);
        	X.list();
        }
        if (tc == 2) {
        	System.out.println("Sort the list of file by using");
        	System.out.println("1. Selection sort");
        	System.out.println("2. Insertion sort");
        	System.out.print("Your choice: ");
        	tc2 = sc.nextInt();
        	System.out.print("Enter a folder : ");
        	str2 = sc.next();
        	f = new File(str2);
        	X.loadFiles(str2);
        
        	if (tc2 == 1) {
        		X.sort(SortType.SELECTIONSORT);
        	}
        	if (tc2 == 2) {
        		X.sort(SortType.INSERTTIONSORT);
        	}
        	X.list();
        }
        if (tc == 3) {
        	System.out.print("Enter any keyword to search: ");
        }
    }
}
//MyFile.java file
import java.io.Serializable;

/**
 *
 * @author CppBuzz.com
 */
public class MyFile implements Serializable {

    // contains information of a File
    
    private String name;
    private long size;
    private String fullPath;
    
    public MyFile() {
    }
    
    public MyFile(String name, long size, String fullPath) {
    	this.name = name;
    	this.size = size;
    	this.fullPath = fullPath;
    }
    
    public String getName() {
    	return name;
    }
    
    public void setName(String name) {
    	this.name = name;
    }
    
    public long getSize() {
    	return size;
    }
    
    public void setSize(long size) {
    	this.size = size;
    }
    
    public String getFullPath() {
    	return fullPath;
    }
    
    public void setFullPath(String fullPath) {
    	this.fullPath = fullPath;
    }
    
    @Override
    public String toString() {
    	return String.format("%-20s%-10d", name, size);
    }

}
//SortType.java file
/**
 *
 * @author CppBuzz.com
 */
public enum SortType {
	SELECTIONSORT, INSERTTIONSORT
}
sort files in java