/* svuk002@ec.auckland.ac.nz */ import java.io.*; import java.sql.Date; public class DirInfoAnswer { private String path; /* Path to the directory */ private static String FILENAME = "dinf"; public DirInfoAnswer (String path) { this.path = path; processDir (this.path); } /* You need to complete this method. * */ private void processDir (String path) { try { BufferedWriter br = new BufferedWriter (new FileWriter (path + "/" + FILENAME)); File directory = new File (path); File[] contents = directory.listFiles (); br.write ("Name Type Size Last Modified\n"); br.write ("\n"); for (int i = 0; i < contents.length; i++) { if (!contents[i].getName ().equals (FILENAME)) { String name = contents[i].getName (); String type = contents[i].isDirectory () ? "dir" : "file"; String size = "" + contents[i].length (); Date lastModified = new Date (contents[i].lastModified ()); String mod = lastModified.toString (); br.write (formatEntry (name, type, size, mod) + "\n"); } } br.close (); } catch (FileNotFoundException e) { e.printStackTrace (); } catch (IOException e) { e.printStackTrace (); } } /* Method below crudely formats an entry. Not to be modified. */ private String formatEntry (String name, String type, String size, String modTime) { String toRet = name; int n = name.length (); for (int i = 0; i < 20 - n; i++) toRet += " "; toRet += type; n = type.length (); for (int i = 0; i < 7 - n; i++) toRet += " "; toRet += size; n = size.length (); for (int i = 0; i < 10 - n; i++) toRet += " "; toRet += modTime; return toRet; } /* You don't need to modify the main method */ public static void main (String[] args) { try { new DirInfoAnswer (args[0]); } catch (ArrayIndexOutOfBoundsException e) { printUsage (); } } /* You don't need to modify this method */ public static void printUsage () { System.out.println ("Usage: java DirInfo dir_name"); } }