/* * Copyright (c) 1999 Rob Quince * Author: Rob Quince * EMail : robq@fiendish.demoncouk * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the * above copyright notice and the following two paragraphs appear in all * copies of this software and that no charge is made for the software. * * IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE * AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER * IS ON AN "AS IS" BASIS, AND THE AUTHOR HAS NO OBLIGATION TO PROVIDE * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. * */ package ramicro.directory; import java.io.*; import java.util.Vector; class Lister implements FilenameFilter { // Include a main in order that this class // can be run by itself to list a directory public static void main(String[] args) { Lister dirList; try { if (args.length == 0) dirList = new Lister("."); else if (args.length == 1) dirList = new Lister(args[0]); else dirList = new Lister(args[0], args); for (int i = 0; i < dirList.files.size(); i++) System.out.println(dirList.files.elementAt(i)); } catch (IOException io) { System.err.println(io); } } // Filtering lister filtering starts at // filter[1] to enable simple argument vector // passing public Lister(String dirName, String[] filter) throws IOException { String[] fArray; dir = new File(dirName); if (!dir.isDirectory()) throw new IOException("Is not a directory"); for (int f = 1; f < filter.length; f++) { dirFilter = filter[f]; fArray = dir.list(this); for (int i = 0; i < fArray.length; i++) insertAlpha(fArray[i]); } } public Lister(String dirName) throws IOException { String[] fArray; dir = new File(dirName); if (!dir.isDirectory()) throw new IOException("Is not a directory"); fArray = dir.list(); for (int i = 0; i < fArray.length; i++) insertAlpha(fArray[i]); } // For the FilenameFilter interface public boolean accept(File d, String n) { return n.toLowerCase().endsWith(dirFilter); } // Simple alpha sorting right now private void insertAlpha(String name) { for (int i = 0; i < files.size(); i++) if (((String )files.elementAt(i)).compareTo(name) > 0) { files.insertElementAt(name, i); return; } files.addElement(name); } // Make the files vector public for now public Vector files = new Vector(20, 20); private File dir; private String dirFilter; }