Commits

dloy committed e277bde Merge

Merge

  • Participants
  • Parent commits e85d0a4, 14dc9e5

Comments (0)

Files changed (1)

core/src/main/java/org/cdlib/mrt/utility/FileUtil.java

         }
     }
 
+
+    /**
+     * Copy all files in a directory to another directory (will overwrite)
+     * @param sourceLocation source for copy
+     * @param targetLocation target for copy
+     * @throws org.cdlib.mrt.utility.MException
+     */
+    public static void copyDirectory(File sourceLocation, File targetLocation)
+        throws TException
+    {
+        copyDirectory(sourceLocation, targetLocation, true);
+    }
+
+
+    /**
+     * Copy all files in a directory to another directory (will not overwrite)
+     * @param sourceLocation source for copy
+     * @param targetLocation target for copy
+     * @throws org.cdlib.mrt.utility.MException
+     */
+    public static void updateDirectory(File sourceLocation, File targetLocation)
+        throws TException
+    {
+        copyDirectory(sourceLocation, targetLocation, false);
+    }
+
+
     /**
      * Copy all files in a director to another directory
      * @param sourceLocation source for copy
      * @param targetLocation target for copy
+     * @param overwrite logical for overwriting existing target file
      * @throws org.cdlib.mrt.utility.MException
      */
-    public static void copyDirectory(File sourceLocation , File targetLocation)
+    private static void copyDirectory(File sourceLocation , File targetLocation, boolean overwrite)
         throws TException
     {
         try {
                 String[] children = sourceLocation.list();
                 for (int i=0; i<children.length; i++) {
                     copyDirectory(new File(sourceLocation, children[i]),
-                            new File(targetLocation, children[i]));
+                            new File(targetLocation, children[i]), overwrite);
                 }
             } else {
-
                 InputStream in = new FileInputStream(sourceLocation);
-                stream2File(in, targetLocation);
+		if (! overwrite && targetLocation.exists()) {}
+                else stream2File(in, targetLocation);
             }
 
         } catch(TException mfe) {
      */
     public static void removeLineFromFile(String file, String lineToRemove, String linePortion) {
 
-        try {
+    try {
 
-          File inFile = new File(file);
+      File inFile = new File(file);
+      
+      if (!inFile.isFile()) {
+        System.out.println("Parameter is not an existing file");
+        return;
+      }
+       
+      //Construct the new file that will later be renamed to the original filename.
+      File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
+      
+      BufferedReader br = new BufferedReader(new FileReader(file));
+      PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
+      
+      String line = null;
 
-          if (!inFile.isFile()) {
-            System.out.println("Parameter is not an existing file");
-            return;
-          }
+      //Read from the original file and write to the new
+      //unless content matches data to be removed.
+      while ((line = br.readLine()) != null) {
+        
+        if ( ! ((line.trim().endsWith(lineToRemove) && linePortion.equals("END"))  ||
+            (line.trim().startsWith(lineToRemove) && linePortion.equals("BEGIN")) ||
+            (line.trim().equals(lineToRemove)))) {
 
-          //Construct the new file that will later be renamed to the original filename.
-          File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
-
-          BufferedReader br = new BufferedReader(new FileReader(file));
-          PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
-
-          String line = null;
-
-          //Read from the original file and write to the new
-          //unless content matches data to be removed.
-          while ((line = br.readLine()) != null) {
-
-            if ( ! ((line.trim().endsWith(lineToRemove) && linePortion.equals("END"))  ||
-                (line.trim().startsWith(lineToRemove) && linePortion.equals("BEGIN")) ||
-                (line.trim().equals(lineToRemove)))) {
-
-              pw.println(line);
-              pw.flush();
-            }
-          }
-          pw.close();
-          br.close();
-
-          //Delete the original file
-          if (!inFile.delete()) {
-            System.out.println("Could not delete file");
-            return;
-          }
-
-          //Rename the new file to the filename the original file had.
-          if (!tempFile.renameTo(inFile))
-            System.out.println("Could not rename file");
-
+          pw.println(line);
+          pw.flush();
         }
+      }
+      pw.close();
+      br.close();
+      
+      //Delete the original file
+      if (!inFile.delete()) {
+        System.out.println("Could not delete file");
+        return;
+      }
+      
+      //Rename the new file to the filename the original file had.
+      if (!tempFile.renameTo(inFile))
+        System.out.println("Could not rename file");
+      
+    }
     catch (FileNotFoundException ex) {
       ex.printStackTrace();
     }
     }
   }
 
-    /**
-     * Return array of nonempty lines from a file
-     * @param logger merritt logger
-     * @param urlS url to file to be split
-     * @return array of lines from original file
-     * @throws TException
-     */
-    public static String[] getLinesFromFile(File splitFile)
-        throws TException
-    {
-        try {
-            String fileContent = FileUtil.file2String(splitFile);
-            if (StringUtil.isEmpty(fileContent)) return null;
-            String [] lines = fileContent.split("[\\n\\r]+");
-            if (lines.length == 0) return null;
-            Vector<String> list = new Vector(lines.length);
-            for (String line: lines) {
-                if (StringUtil.isNotEmpty(line)) list.add(line);
-            }
-            return list.toArray(new String[0]);
-
-        } catch (Exception ex) {
-            return null;
-
-        }
-    }
-
-    /**
-     * Return array of nonempty lines from url content
-     * @param logger merritt logger
-     * @param urlS url to file to be split
-     * @return array of lines from original file
-     * @throws TException
-     */
-    public static String[] getLinesFromURL(LoggerInf logger, String urlS)
-        throws TException
-    {
-        File tempFile = null;
-        try {
-            tempFile = url2TempFile(logger, urlS);
-            return getLinesFromFile(tempFile);
-
-        } catch (Exception ex) {
-            return null;
-
-        } finally {
-            if (tempFile != null) {
-                try {
-                    tempFile.delete();
-                } catch (Exception ex) { }
-            }
-        }
-    }
-
 }