// ============================================================================= // FileCollection class // This class is a collection of all the files in the directory it was // passed. The files can then be filtered, copied, or deleted. // // *Note to grader - This file is from a program I wrote last week to // convert SHTML files with server side includes into standard HTML // files. The program was written to aid in the development of my new // website (http://www.netbeetle.com). // // Methods // public FileCollection(File directory) // - loads all files in the directory into the collection // public void filter(String[] searchString) // - filters out all files that do not meet any of the search strings // public void filter(String searchString) // - filters out all files that do not meet the search string // public void filterOut(String searchString) // - filters out all files that match the search string // public String toString() - returns a string of all the files // public File getDirectory() - returns the parent directory // public File get(int index) - returns the file at the index position // public int size() - returns the number of files in the collection // public void copy(File newDirectory) - copies the entire directory // public void deleteAll() - deletes all files in the collection // public void delete(int index) - deletes a file in the collection // public void remove(int index) - removes a file from the collection // public boolean add(File file) - adds a file to the collection // public int indexOf(File file) - returns the index of a file // public int contains(File file) - returns true if file is in collection // // Variables // protected Vector filesVector - the vector of files in the collection // protected File directory - the directory that all the files are in // ============================================================================= import java.io.*; import java.util.*; public class FileCollection { // ------------------------------------------------------------------------- // Variables // ------------------------------------------------------------------------- protected Vector filesVector; protected File directory; // ------------------------------------------------------------------------- // Constructor // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- // public FileCollection(File directory) // Loads all the files in the directory into the collection. // ------------------------------------------------------------------------- public FileCollection(File directory) { this.directory = directory.getAbsoluteFile(); filesVector = new Vector(); if (!directory.isDirectory()) return; File[] filesArray = directory.listFiles(); for (int i = 0; i < filesArray.length; i++) filesVector.add(filesArray[i].getAbsoluteFile()); Collections.sort(filesVector); File[] directoryArray; Vector directoryVector; for (int i = 0; i < filesVector.size(); i++) { if (((File) filesVector.get(i)).isDirectory()) { directoryArray = ((File) filesVector.get(i)).listFiles(); directoryVector = new Vector(); for (int j = 0; j < directoryArray.length; j++) directoryVector.add(directoryArray[j].getAbsoluteFile()); Collections.sort(directoryVector); filesVector.addAll(i + 1, directoryVector); } } } // ------------------------------------------------------------------------- // Methods // ------------------------------------------------------------------------- // ------------------------------------------------------------------------- // public void filter(String[] searchString) // Removes all files from the collection that do not match at least one // of the strings in the array. // ------------------------------------------------------------------------- public void filter(String[] searchString) { for (int i = 0; i < filesVector.size(); i++) { String filename = ((File) filesVector.get(i)).getPath(); boolean match = false; for (int j = 0; j < searchString.length; j++) { if (filename.indexOf(searchString[j]) > -1) { match = true; break; } } if (match == false) { filesVector.remove(i); i--; } } } // ------------------------------------------------------------------------- // public void filter(String searchString) // Calls filter(String[] searchString) after making the string into // an array of length one. // ------------------------------------------------------------------------- public void filter(String searchString) { String[] searchStringArray = {searchString}; filter(searchStringArray); } // ------------------------------------------------------------------------- // public void filterOut(String searchString) // Removes all files from the collection that match the search string. // ------------------------------------------------------------------------- public void filterOut(String searchString) { searchString = searchString.toLowerCase(); String filename; for (int i = 0; i < filesVector.size(); i++) { filename = ((File) filesVector.get(i)).getName().toLowerCase(); if (filename.indexOf(searchString) > -1) { filesVector.remove(i); i--; } } } // ------------------------------------------------------------------------- // public String toString() // Returns a string of all files in the collection. // ------------------------------------------------------------------------- public String toString() { return filesVector.toString(); } // ------------------------------------------------------------------------- // public File getDirectory() // Gets the parent directory of all files in the collection. // ------------------------------------------------------------------------- public File getDirectory() { return directory; } // ------------------------------------------------------------------------- // public File get(int index) // Gets the file at the index position. // ------------------------------------------------------------------------- public File get(int index) { return (File) filesVector.get(index); } // ------------------------------------------------------------------------- // public int size() // Returns the size of the collection. // ------------------------------------------------------------------------- public int size() { return filesVector.size(); } // ------------------------------------------------------------------------- // public void copy(File newDirectory) // Copyies all the files in the collection to a new directory while // preserving the directory structure between the files and the // parent directory. // ------------------------------------------------------------------------- public void copy(File newDirectory) throws IOException { newDirectory = newDirectory.getAbsoluteFile(); newDirectory.mkdirs(); int directoryLength = directory.toString().length(); String filename; File inFile; File outFile; FileInputStream reader; FileOutputStream writer; byte[] bytes = new byte[1024]; byte[] endBytes; int length; for (int i = 0; i < filesVector.size(); i++) { inFile = (File) filesVector.get(i); filename = inFile.getPath(); filename = filename.substring(directoryLength, filename.length()); outFile = new File(newDirectory, filename); if (inFile.isFile()) { outFile.getParentFile().mkdirs(); reader = new FileInputStream(inFile); writer = new FileOutputStream(outFile); length = reader.read(bytes); while (length == 1024) { writer.write(bytes); length = reader.read(bytes); } if (length > 0) { endBytes = new byte[length]; for (int j = 0; j < length; j++) endBytes[j] = bytes[j]; writer.write(endBytes); } reader.close(); writer.close(); } else if (inFile.isDirectory()) outFile.mkdirs(); filesVector.remove(i); filesVector.add(i, outFile); } directory = newDirectory; } // ------------------------------------------------------------------------- // public void deleteAll() // Deletes all the files in the collection off of the hard drive and // empties the collection. // ------------------------------------------------------------------------- public void deleteAll() { File file; for (int i = 0; i < filesVector.size(); i++) { file = (File) filesVector.get(i); file.delete(); } filesVector = new Vector(); } // ------------------------------------------------------------------------- // public void delete(int index) // Deletes the files at the specified index postion. // ------------------------------------------------------------------------- public void delete(int index) { File file = (File) filesVector.get(index); file.delete(); filesVector.remove(index); } // ------------------------------------------------------------------------- // public void remove(int index) // Removes the files at the specified index postion. // ------------------------------------------------------------------------- public void remove(int index) { filesVector.remove(index); } // ------------------------------------------------------------------------- // public boolean add(File file) // Adds the files specified and resorts the collection. Returns true // if the file is added, false otherwise. // ------------------------------------------------------------------------- public boolean add(File file) { file = file.getAbsoluteFile(); File fileDirectory = file.getParentFile(); boolean inDirectory = false; while (fileDirectory != null) { if (fileDirectory.equals(directory)) inDirectory = true; fileDirectory = fileDirectory.getParentFile(); } if (inDirectory && file.exists() && !contains(file)) { filesVector.add(file); Collections.sort(filesVector); return true; } return false; } // ------------------------------------------------------------------------- // public int indexOf(File file) // Returns the index of the file, or -1 if it is not in the collection. // ------------------------------------------------------------------------- public int indexOf(File file) { file = file.getAbsoluteFile(); return filesVector.indexOf(file); } // ------------------------------------------------------------------------- // public boolean contains(File file) // Returns true if the file is in the collection. // ------------------------------------------------------------------------- public boolean contains(File file) { file = file.getAbsoluteFile(); return filesVector.contains(file); } }