/* * Copyright (c) 2001 Matthew Feldt. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided the copyright notice above is * retained. * * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESSED OR * IMPLIED WARRANTIES. */ /** * DirectoryList.java * * Java Examples In A Nutshell Copyright (c) 2000 David Flanagan * Exercise 3-5: * Write a program that lists all of the files and subdirectories in a * specified directory, along with their sizes and modification dates. * By default, the output should be sorted by name. If invoked with the * -s option, however, output should be sorted by size from largest to * smallest. If invoked with the -d option, output should be sorted by * date, from most recent to least. Use the sort() method of * java.util.Collections to help with the sorting. * * @author Matthew Feldt * @version 1.0, 01/31/2001 22:18 */ package com.feldt.examples.io; import java.lang.*; import java.io.*; import java.util.*; import java.text.*; class DirectoryList { /** implementation of Comparator that sorts by file size */ static class FileSizeComparator implements Comparator { public int compare(Object a, Object b) { return (int)(((File)a).length() - ((File)b).length()); } public boolean equals(Object a) { return (compare(this, a) == 0); } } /** implementation of Comparator that sorts by file modification time */ static class FileDateComparator implements Comparator { public int compare(Object a, Object b) { return (int)(((File)a).lastModified() - ((File)b).lastModified()); } public boolean equals(Object a) { return (compare(this, a) == 0); } } /** * Static method to print a pleasing list of files and subdirectories * from a specified directory sorted by name, date or size. */ public static void printList(File dir, int sortby) throws IllegalArgumentException { final int WIDTH = 13; final char[] spaces = " ".toCharArray(); SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a "); DecimalFormat nf = new DecimalFormat("#,##0 "); StringBuffer result; FieldPosition pos = new FieldPosition(NumberFormat.INTEGER_FIELD); List dirList = new Vector(); long total = 0; // validate directory argument if (! dir.exists()) throw new IllegalArgumentException(dir + " does not exist."); if (! dir.canRead()) throw new IllegalArgumentException(dir + " read protected."); // populate List with files from the specified directory File[] entries = dir.listFiles(); for (int i = 0; i < entries.length; i++) { dirList.add(entries[i]); } // determine which sort to use and sort List switch (sortby) { case 0: java.util.Collections.sort(dirList); break; case 1: java.util.Collections.sort(dirList, new FileSizeComparator()); break; case 2: java.util.Collections.sort(dirList, new FileDateComparator()); break; } // print the result using SimpleDateFormat and DecimalFormat // to condition the output for (int i = 0; i < dirList.size(); i++) { result = new StringBuffer(32); File f = (File)dirList.get(i); System.out.print(df.format(new Date(f.lastModified()))); // date if (f.isDirectory()) System.out.print(" "); // directory else System.out.print(" "); nf.format(f.length(), result, pos); // size if (pos.getEndIndex() < WIDTH) // align with spaces if necessary result.insert(0, spaces, 0, WIDTH-pos.getEndIndex()); System.out.print(result); System.out.println(f.getName()); // name } } /** test class */ static class Test { public static void main (String args[]) { final String usage = "Usage: java DirectoryList$Test [[-s] || [-d]] dir"; int sortby = 0, fileNameIndex = 0; switch (args.length) { case 1: { // use option and fileNameIndex defaults break; } case 2: { if (args[0].equals("-s")) { // sort by size sortby = 1; fileNameIndex = 1; break; } else if (args[0].equals("-d")) { // sort by date sortby = 2; fileNameIndex = 1; break; } // otherwise fall through to the error message } default: { System.err.println(usage); System.exit(-1); } } // print the directory listing DirectoryList.printList(new File(args[fileNameIndex]), sortby); } } }