Here's a pretty useful app. It took me a whole of 15 minutes to code it and debug it, so don't expect too much from the design.
This is taken from the help:
Usage: java -cp <path> PackageChanger
Changes all *.java files in the current directory, and recursively, to have the package name of <new package name> + the descended directory name.
If no package is given, then the current directory is considered the base path, and all directories are the new packages.
The "package" text must be on the first column, and the old package name must be on the same line, ending with a semicolon (';'). If these details are not fulfilled, then the file's package name is not changed or added.
There are two ways of using this tool: * Inside a package directory, you can run the app, with the command line argument specifying what the current directory's package name is, or * In a base directory, where each directory is a root package name.
Have fun!
import java.io.*; public class PackageChanger implements Runnable { public static void main( String args[] ) { String newPackageName = parsePackage( args ); if (newPackageName == null) return; PackageChanger pc = new PackageChanger( newPackageName ); pc.run(); } String newName; public PackageChanger( String newPackageName ) { System.out.println("Using new base package name of "+newPackageName); newName = newPackageName; } public void run() { // Find all files recurse( new File("."), newName ); } /** * File must be a directory. */ protected void recurse( File f, String newPackage ) { File fl[] = f.listFiles(); if (fl == null) { changeFile( f, newPackage ); return; } if (!f.getName().equals(".")) { if (newPackage.length() > 0) { // only put the period there if there is a // previous package name. newPackage += '.'; } newPackage += f.getName(); } int i = 0, len = fl.length; for (; i < len; i++) { if (fl[i].isDirectory()) { recurse(fl[i], newPackage); } else { changeFile( fl[i], newPackage ); } } } protected void changeFile( File f, String newPackage ) { // check if it's a java file if (!f.isFile() || !f.getName().endsWith(".java")) { return; } System.out.println("Adjusting file "+f); File temp = new File( f.getName()+".temp.PackageChanger" ); try { PrintWriter pr = new PrintWriter( new FileWriter( temp ) ); BufferedReader br = new BufferedReader( new FileReader( f ) ); String line = br.readLine(); boolean notDonePackage = true; int pos; while (line != null) { if (line.startsWith( "package " )) { if (notDonePackage) { pos = line.indexOf(';'); if (pos > 0) { System.out.println(" old package line was "+line); if (newPackage.length() <= 0) { // comment out the old package line line = "// No longer in a package.n"+ "// "+line; } else { line = "package "+newPackage+line.substring(pos); } System.out.println(" new package line is "+line); notDonePackage = false; } } else { System.out.println( " Warning: there was another package line in the "+ "file. It was ignored."); } } pr.println( line ); line = br.readLine(); } // copy temp to f pr.close(); br.close(); pr = new PrintWriter( new FileWriter( f ) ); br = new BufferedReader( new FileReader( temp ) ); line = br.readLine(); while ( line != null ) { pr.println( line ); line = br.readLine(); } pr.close(); br.close(); // delete the temporary file temp.delete(); } catch (IOException ioe) { ioe.printStackTrace(); } } private static final String parsePackage( String args[] ) { if (args.length <= 0) { // this is the root directory return ""; } if (args[0].equals("?") || args[0].equals("/?") || args[0].equals("-h") || args[0].equals("/h") || args[0].equals("-?")) { System.out.println("PackageChanger:n"+ "Usage: java -cp <path> PackageChanger <new package name>nn"+ "Changes all *.java files in the current directory, and recursively,n"+ "to have the package name of <new package name> + the descended directoryn"+ "name.nn"+ "If no package is given, then the current directory is considered then"+ "base path, and all directories are the new packages.nn"+ "The "package" text *must* be on the first column, and the old packagen"+ "name *must* be on the same line, ending with a semicolon (';'). If thesen"+ "details are not fulfilled, then the file's package name is not changedn"+ "or added." ); return null; } return args[0]; } }
-- MattAlbrecht - 30 Aug 1999