Question
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
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:
java -jar hw5.jar [directory_name] [-a | -l | -s]
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:
usage: java -jar hw5.jar [directory] [-a | -l | -s] (current directory is default) -a alphabetical sorting -l last time modified sorting -s file size
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
> java -jar hw5.jar 4096 May 26 14:51:35 yadda 21 May 26 14:53:39 manifest 424 May 27 14:09:03 MyCode$1.class 535 May 27 14:09:03 MyCode$2.class 2489 May 27 14:09:03 MyCode.class 2845 May 27 15:11:31 hw5.jar 4391 May 27 14:08:57 MyCode.java 4323 May 26 14:48:17 MyCode.java~ 822 May 26 15:05:12 testcases > java -jar hw5.jar yadda -a 9 May 26 14:51:35 a.txt 12 May 26 14:51:22 b.txt 7 May 26 14:51:04 c.txt > java -jar hw5.jar yadda -l 7 May 26 14:51:04 c.txt 12 May 26 14:51:22 b.txt 9 May 26 14:51:35 a.txt > java -jar hw5.jar yadda -s 7 May 26 14:51:04 c.txt 9 May 26 14:51:35 a.txt 12 May 26 14:51:22 b.txt
Some test runs illustrating error cases
> java -jar hw5.jar yadda -a -l usage: java -jar hw5.jar [directory] [-a | -l | -s] (current directory is default) -a alphabetical sorting -l last time modified sorting > java -jar hw5.jar yadda -a -x usage: java -jar hw5.jar [directory] [-a | -l | -s] (current directory is default) -a alphabetical sorting -l last time modified sorting > java -jar hw5.jar blah Invalid directory name usage: java -jar hw5.jar [directory] [-a | -l | -s] (current directory is default) -a alphabetical sorting -l last time modified sorting > java -jar hw5.jar -a yadda usage: java -jar hw5.jar [directory] [-a | -l | -s] (current directory is default) -a alphabetical sorting -l last time modified sorting
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started