/* * Converter.java * * Created on February 12, 2007, 3:57 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ //package appletconverter; import java.io.*; import java.util.Vector; /** *Reads all java files in a directory specified by the users and removes all instances of package. * * @author Matt DesVoigne */ public class Converter { public static final String OMITSTRING = "package"; private FilenameFilter filter; private File[] files; private String directory; /** * Creates a new instance of Converter */ public Converter(String directory) { filter = new FilenameFilter() { public boolean accept(File dir, String name) { return (name.endsWith("java") /*|| (new File(dir, name).isDirectory())*/); } }; this.directory = directory; File dir = new File(directory); System.out.println("Directory is " + dir.getAbsolutePath()); if (!dir.isDirectory()) { throw new IllegalArgumentException("Converter: No such directory: " + dir.getAbsolutePath()); } // Get the filtered file names. files = dir.listFiles(filter); /* for (int i=0; i < files.length; i++) { System.out.println(files[i].getAbsolutePath()); } */ } /** *For each file in the list of file names, remove any lines which when trimmed start with the word "package." */ public void editFiles() { for (int i=0; i < files.length; i++) { // Read the contents of each file into a vector, omitting any lines beginning with the word "package." Vector lines = new Vector(); BufferedReader in; try { in = new BufferedReader( new InputStreamReader( new BufferedInputStream( new FileInputStream(files[i])))); } catch (FileNotFoundException fnfe) { System.out.println("The file: " + files[i].getAbsolutePath() + " cannot be found. Returning empty galaxsee."); return; } String input; try { while ((input = in.readLine()) != null) { if (!input.trim().startsWith(OMITSTRING)) { // Ignore any lines which begin with the word "package." lines.addElement(input); } } } catch (IOException ioe) { System.out.println("ERROR: Parsing data file " + files[i].getAbsolutePath()); System.out.println(ioe.getMessage()); ioe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } try { in.close(); } catch (IOException ioe) { System.out.println("Error closing files[i]."); System.out.println(ioe.getMessage()); ioe.printStackTrace(); } // Now write the contents of the lines vector back to the file. PrintWriter out; try { out = new PrintWriter( new BufferedOutputStream( new FileOutputStream(files[i]))); } catch (FileNotFoundException fnfe) { System.out.println("Error saving file: File Not Found:" + files[i].getAbsolutePath() + ", " + fnfe.getMessage()); fnfe.printStackTrace(); return; } try { for (int j=0; j