[507] | 1 | /*
|
---|
| 2 | * To change this template, choose Tools | Templates
|
---|
| 3 | * and open the template in the editor.
|
---|
| 4 | */
|
---|
| 5 | package gov.hhs.fha.nhinc.devtools;
|
---|
| 6 |
|
---|
| 7 | import java.io.File;
|
---|
| 8 | import java.io.FileInputStream;
|
---|
| 9 | import java.io.FileOutputStream;
|
---|
| 10 | import java.io.IOException;
|
---|
| 11 | import java.io.InputStream;
|
---|
| 12 | import java.io.OutputStream;
|
---|
| 13 |
|
---|
| 14 | /**
|
---|
| 15 | *
|
---|
| 16 | * @author westbergl
|
---|
| 17 | */
|
---|
| 18 | public class FindAndReplaceFile
|
---|
| 19 | {
|
---|
| 20 | /**
|
---|
| 21 | * Copy contents of the file from the src to the dest. If the dest exists, it will
|
---|
| 22 | * be deleted first.
|
---|
| 23 | *
|
---|
| 24 | * @param fSrc The source file
|
---|
| 25 | * @param fDst The destination file.
|
---|
| 26 | * @return true if the file was copied.
|
---|
| 27 | * @throws java.io.IOException
|
---|
| 28 | */
|
---|
| 29 | private static boolean copy(File fSrc, File fDst)
|
---|
| 30 | throws IOException
|
---|
| 31 | {
|
---|
| 32 | if (!fSrc.exists())
|
---|
| 33 | {
|
---|
| 34 | return false;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | if (fDst.exists())
|
---|
| 38 | {
|
---|
| 39 | // Delete the file first....
|
---|
| 40 | fDst.delete();
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | InputStream oIn = new FileInputStream(fSrc);
|
---|
| 44 | OutputStream oOut = new FileOutputStream(fDst);
|
---|
| 45 |
|
---|
| 46 | // Transfer bytes from in to out
|
---|
| 47 | byte[] buf = new byte[1024];
|
---|
| 48 | int iLen;
|
---|
| 49 | while ((iLen = oIn.read(buf)) > 0)
|
---|
| 50 | {
|
---|
| 51 | oOut.write(buf, 0, iLen);
|
---|
| 52 | }
|
---|
| 53 | oIn.close();
|
---|
| 54 | oOut.close();
|
---|
| 55 |
|
---|
| 56 | return true;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * This method looks in the current directory and all subdirectories for the
|
---|
| 63 | * specified file and replaces it with the copy specified.
|
---|
| 64 | *
|
---|
| 65 | * @param fDirToLook The directory to start looking.
|
---|
| 66 | * @param fFileName The path and name of the file to copy.
|
---|
| 67 | * @return The number of replacements that were made.
|
---|
| 68 | * @throws java.lang.Throwable
|
---|
| 69 | */
|
---|
| 70 | public static int searchAndReplace(File fDirToLook, File fFileName)
|
---|
| 71 | throws Throwable
|
---|
| 72 | {
|
---|
| 73 | if (fDirToLook == null)
|
---|
| 74 | {
|
---|
| 75 | return 0;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | if (fDirToLook.isDirectory())
|
---|
| 79 | {
|
---|
| 80 | int iCopies = 0;
|
---|
| 81 |
|
---|
| 82 | File[] faFilesInDir = fDirToLook.listFiles();
|
---|
| 83 | if ((faFilesInDir != null) && (faFilesInDir.length > 0))
|
---|
| 84 | {
|
---|
| 85 | for (File fFile : faFilesInDir)
|
---|
| 86 | {
|
---|
| 87 | iCopies += searchAndReplace(fFile, fFileName);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | return iCopies;
|
---|
| 92 | }
|
---|
| 93 | else if (fDirToLook.isFile())
|
---|
| 94 | {
|
---|
| 95 | // System.out.println("Debug: fDirToLook.getCanonicalPath()" + fDirToLook.getCanonicalPath());
|
---|
| 96 | if (fDirToLook.getCanonicalPath().equalsIgnoreCase(fFileName.getCanonicalPath()))
|
---|
| 97 | {
|
---|
| 98 | return 0; // Do not change the file itself...
|
---|
| 99 | }
|
---|
| 100 | else if (fDirToLook.getName().equalsIgnoreCase(fFileName.getName()))
|
---|
| 101 | {
|
---|
| 102 | try
|
---|
| 103 | {
|
---|
| 104 | boolean bCopied = copy(fFileName, fDirToLook);
|
---|
| 105 | if (bCopied)
|
---|
| 106 | {
|
---|
| 107 | System.out.println("Replaced file: " + fDirToLook.getCanonicalPath());
|
---|
| 108 | return 1;
|
---|
| 109 | }
|
---|
| 110 | else
|
---|
| 111 | {
|
---|
| 112 | return 0;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | catch (Exception e)
|
---|
| 116 | {
|
---|
| 117 | System.out.println("Failed to replace file: " + fDirToLook.getCanonicalPath() + " Error: " + e.getMessage());
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | return 0;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Print the usage information for this project.
|
---|
| 127 | */
|
---|
| 128 | public static void printUsage()
|
---|
| 129 | {
|
---|
| 130 | System.out.println("Usage: gov.hhs.fha.nhinc.devtools.FindAndReplaceFile <dir-to-look> <file>");
|
---|
| 131 | System.out.println("Where:");
|
---|
| 132 | System.out.println("<dir-to-look> is the directory where the tool should start looking for the file.");
|
---|
| 133 | System.out.println(" it will search all sub-directories of this one.");
|
---|
| 134 | System.out.println("<file> the path and location of the file that is to be used for the replacement.");
|
---|
| 135 | System.out.println(" the name of this file is also the name of the file that will be replaced.");
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Main method.
|
---|
| 140 | *
|
---|
| 141 | * @param args
|
---|
| 142 | */
|
---|
| 143 | public static void main(String[] args)
|
---|
| 144 | {
|
---|
| 145 | if (args.length != 2)
|
---|
| 146 | {
|
---|
| 147 | printUsage();
|
---|
| 148 | System.exit(-1);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | String sDirToLook = "";
|
---|
| 152 | String sFileName = "";
|
---|
| 153 | sDirToLook = args[0];
|
---|
| 154 | sFileName = args[1];
|
---|
| 155 |
|
---|
| 156 | File fDirToLook = new File(sDirToLook);
|
---|
| 157 | File fFileName = new File(sFileName);
|
---|
| 158 |
|
---|
| 159 | try
|
---|
| 160 | {
|
---|
| 161 | int iCount = searchAndReplace(fDirToLook, fFileName);
|
---|
| 162 | System.out.println("Replaced " + iCount + " copies of this file.");
|
---|
| 163 | System.exit(0);
|
---|
| 164 | }
|
---|
| 165 | catch (Throwable t)
|
---|
| 166 | {
|
---|
| 167 | System.out.println("An unexpected exception occurred. Error: " + t.getMessage());
|
---|
| 168 | t.printStackTrace();
|
---|
| 169 | System.exit(-1);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | }
|
---|