Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAM Objective: Practice with properties of files and directories, as we

ID: 3725969 • Letter: J

Question

JAVA PROGRAM

Objective:

Practice with properties of files and directories, as well as sorting arrays of objects.

Task:

Create a program that will list the contents of a directory, sorted alphabetically by name or sorted by last modified date. Files and directories are to be output one per row, with the following information: length (in bytes), last modified date, name.

Requirements

Output should look like the sample runs shown below

Input will come into the program through command line arguments. The command format is:

The directory_name is an optional argument. If not provided, the current directory is the default

-a means alphabetical sorting, by file or directory name

-l means sort by "last time modified", ascending order

-s means sort by file length (size), ascending order


If an error on the command line occurs, print a "usage" message (this is very typical for programs with command-line arguments), which should look like this:

If the directory name in the command line is an invalid directory, then print the error message "Invalid directory name" before the usage message, as well.

You may assume that no directories or files begin with the '-' character

Sample runs

For these examples, assume that "yadda" is a directory, and "blah" is not.

Some runs with correct usage

Some test runs illustrating error cases

Explanation / Answer

/*
Copy the below code as java file and runit as java file_listing on terminal
As code is long so before reading the code first see the schema in which I code this problem
start
    take the arguments from command line
    check for error cases
    then I code for case if no directory name is given then we do code with the current directory
    then next case if file name is given
    then check for file name validation
    then code as if no file ordering condition is given(i.e; -a -l -s is not given)
    then code for the case -a
    then code for the case -l
    then code for the case -s
end

In the whole main thing is the three comparators used for the sorting function
*/

import java.io.File;
import java.text.SimpleDateFormat;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class file_listing
{
   
    // Driver Method
    public static void main(String[] args)
    {
        //dir path stored in maindirpath
        String maindirpath ;
      
      
        //for handling the error cases
        if(args.length>2){
            System.out.println("usage: java -jar hw5.jar [directory] [-a | -l | -s]");
            System.out.println("(current directory is default)");
            System.out.println("-a alphabetical sorting");
            System.out.println("-l last time modified sorting");
      
        }

        else {
        //if no command line argument is specified
            if(args.length==0){
                maindirpath=System.getProperty("user.dir");
                File maindir = new File(maindirpath);
            
                if(maindir.exists() && maindir.isDirectory())
                {
                    File files[] = maindir.listFiles();
                    Arrays.sort(files);
                   
                    SimpleDateFormat sdf = new SimpleDateFormat("MMM d HH:mm:ss");
                    for(File f: files){
                        System.out.println((f.length())+" "+ sdf.format(f.lastModified())+" " +f.getName());
                    }
                }
            }
        //if file name is given in the argument
        else {
            maindirpath=args[0];
            //check the file name errors
            File maindir = new File(maindirpath);
            if(!maindir.exists() && !maindir.isDirectory()){
                System.out.println("Invalid directory name");
                System.out.println("usage: java -jar hw5.jar [directory] [-a | -l | -s]");
                System.out.println("(current directory is default)");
                System.out.println("-a alphabetical sorting");
                System.out.println("-l last time modified sorting");
            }else if(args.length==1){
                     if(maindir.exists() && maindir.isDirectory())
                    {
                        File files[] = maindir.listFiles();
                        Arrays.sort(files);
                       
                        SimpleDateFormat sdf = new SimpleDateFormat("MMM d HH:mm:ss");
                        for(File f: files){
                            System.out.println((f.length())+" "+ sdf.format(f.lastModified())+" " +f.getName());
                        }
                    }
            //if name is given as arguments and no other modifier is given
                 
            }  
             else
            {
             if(args[1].equals("-a")){
                  System.out.println("Sorting in alphabetical order is selected");
                  // File maindir = new File(maindirpath);
        
                    if(maindir.exists() && maindir.isDirectory())
                        {
                            File files[] = maindir.listFiles();
                            Arrays.sort(files);
                           
                            SimpleDateFormat sdf = new SimpleDateFormat("MMM d HH:mm:ss");
                             //sort files in alphabetical order with the use of the comparator function
                               Arrays.sort(files,new Comparator()
                                {
                                    @Override
                                    public int compare(Object f1 , Object f2){
                                        return ((File)f1).getName().compareTo(((File)f1).getName());
                                    }
                                });

                                for(File f: files)
                                    System.out.println((f.length())+" "+ sdf.format(f.lastModified())+" " +f.getName());
                              
                        }                              
                    }


             else if(args[1].equals("-l")){
                  System.out.println("Sorting in last modified order");
                  // File maindir = new File(maindirpath);
        
                    if(maindir.exists() && maindir.isDirectory())
                        {
                            File files[] = maindir.listFiles();
                            Arrays.sort(files);
                           
                            SimpleDateFormat sdf = new SimpleDateFormat("MMM d HH:mm:ss");
                             //sort files by last modified date
                               Arrays.sort(files,new Comparator()
                                {
                                    @Override
                                    public int compare(Object F1,Object F2){
                                        if(((File)F1).lastModified() < ((File)F2).lastModified()){
                                            return -1;
                                        }
                                        else if(((File)F1).lastModified() < ((File)F2).lastModified()){
                                            return 1;
                                        }
                                        else return 0;
                                    }
                                });

                                for(File f: files)
                                    System.out.println((f.length())+" "+ sdf.format(f.lastModified())+" " +f.getName());
                        }                              
                    }

            
              else if(args[1].equals("-s")){
                  System.out.println("Sorting in order of their size");
                  // File maindir = new File(maindirpath);
        
                    if(maindir.exists() && maindir.isDirectory())
                        {
                            File files[] = maindir.listFiles();
                            Arrays.sort(files);
                           
                            SimpleDateFormat sdf = new SimpleDateFormat("MMM d HH:mm:ss");
                             //sort files by size
                               Arrays.sort(files,new Comparator()
                                {
                                    @Override
                                    public int compare(Object F1,Object F2){
                                        if(((File)F1).length() < ((File)F2).length()){
                                            return -1;
                                        }
                                        else if(((File)F1).length() < ((File)F2).length()){
                                            return 1;
                                        }
                                        else return 0;
                                    }
                                });

                                for(File f: files)
                                    System.out.println((f.length())+" "+ sdf.format(f.lastModified())+" " +f.getName());
                              
                            }                              
                        }
                    }
                }
             }    
        }
}