Class FileUtil

java.lang.Object
org.cdlib.mrt.utility.FileUtil

public class FileUtil extends Object
Generalized file utilities
Author:
dloy
  • Field Details

  • Constructor Details

    • FileUtil

      public FileUtil()
  • Method Details

    • url2File

      public static void url2File(LoggerInf m_logger, URL fileURL, File outFile) throws TException
      Get content referenced by a url and save in a file
      Parameters:
      m_logger - logger
      fileURL - link to file to be extracted
      outFile - target file to create
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • url2File

      public static void url2File(LoggerInf m_logger, String urlS, File outFile) throws TException
      Get content referenced by a url and save in a file
      Parameters:
      m_logger - logger
      urlS - link to file to be extracted
      outFile - target file to create
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • url2File

      public static void url2File(LoggerInf m_logger, URL fileURL, File outFile, int retry) throws TException
      Get content referenced by a url and save in a file
      Parameters:
      m_logger - logger
      fileURL - link to file to be extracted
      outFile - target file to create
      retry - number of retry attempts
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • url2File

      public static void url2File(LoggerInf m_logger, String urlS, File outFile, int retry) throws TException
      Get content referenced by a url and save in a file
      Parameters:
      m_logger - logger
      urlS - link to file to be extracted
      outFile - target file to create
      retry - number of retry attempts
      Throws:
      org.cdlib.mcur.utility.MException
      TException
    • url2TempFile

      public static File url2TempFile(LoggerInf m_logger, String urlS) throws TException
      get remote file
      Parameters:
      manifestFile -
      Returns:
      Throws:
      org.cdlib.framework.utility.FrameworkException
      TException
    • stream2File

      public static void stream2File(InputStream inStream, File outFile) throws TException
      Create a file from a stream
      Parameters:
      inStream - stream used to create file
      outFile - file to create
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • url2OutputStream

      public static void url2OutputStream(String urlS, OutputStream outStream, int retry) throws TException
      Move url response to output stream
      Parameters:
      urlS - link to file to be extracted
      outStream - output stream
      retry - number of retry attempts
      Throws:
      org.cdlib.mcur.utility.MException
      TException
    • stream2Stream

      public static void stream2Stream(InputStream inStream, OutputStream outStream) throws TException
      Copy input stream to output stream
      Parameters:
      inStream - - input stream
      outStream - - output Stream
      Throws:
      TException - process exception
    • copyFile

      public static void copyFile(String fileName, File sourceDirectory, File targetDirectory) throws TException
      Copy file from one directory to another
      Parameters:
      fileName - string name of file (may include some path extensions)
      sourceDirectory - from directory
      targetDirectory - to directory
      Throws:
      TException
    • file2file

      public static void file2file(File sourceFile, File targetFile) throws TException
      Copy one file to another
      Parameters:
      sourceFile - input file
      targetFile - output file
      Throws:
      TException - service exception
    • getURLEncodeFilePath

      public static String getURLEncodeFilePath(String filePath)
      Build a URL equivalent path using a java file path This routine maintains all slashes in the file path as a slash in the URL. All other special charecters are url encoded
      Parameters:
      filePath - file path to convert to a URL form
      Returns:
      URI valid path
    • copyReg

      public static void copyReg(String fileExp, File sourceDirectory, File targetDirectory) throws TException
      Copy files in one directory to another directory based on matchin a regular expression. If a directory matches the expression then it will also be copied
      Parameters:
      fileExp - regex used for matching the fileName
      sourceDirectory - source directory for copy
      targetDirectory - target directory for copy
      Throws:
      TException - process exception
    • string2File

      public static void string2File(File createFile, String fileContent) throws TException
      Create a file containing this string value (utf-8)
      Parameters:
      createFile - file to create
      fileContent - string containing content to save in file
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • file2String

      public static String file2String(File extractFile) throws TException
      Extract the value of file to a string (utf-8)
      Parameters:
      extractFile - file to be extracted
      Returns:
      string form of file content
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • file2String

      public static String file2String(File extractFile, String encType) throws TException
      Extract the value of file to a string (utf-8)
      Parameters:
      extractFile - file to be extracted
      encType - bit to character encoding
      Returns:
      string form of file content
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • getTempFile

      public static File getTempFile(String prefix, String suffix) throws TException
      Create a temp file using prefix and suffix (THREAD SAFE)
      Parameters:
      iStream - InputStrea of data to be copied
      prefix - prefix of temp file name
      suffix - suffix of temp file name
      Throws:
      TException
    • deleteTempFile

      public static Boolean deleteTempFile(File testFile)
      Delete file if is temp
      Parameters:
      testFile - temp file to be deleted
      Returns:
      true=file no longer exists on completion; false=file not deleted
    • isTempFile

      public static boolean isTempFile(File file)
      is this a temp file
      Parameters:
      file -
      Returns:
      true=is temp; false=is not temp
    • getTempDir

      public static File getTempDir(String directoryPrefix) throws TException
      Get a temporary directory
      Parameters:
      directoryPrefix - prefix for temporary file
      Returns:
      temp file
      Throws:
      TException
    • getTempDir

      public static File getTempDir(String directoryPrefix, LoggerInf logger) throws TException
      Get a temporary directory
      Parameters:
      directoryPrefix - prefix for temporary file
      logger - log file
      Returns:
      temporary directory
      Throws:
      TException
    • copyDirectory

      public static void copyDirectory(File sourceLocation, File targetLocation) throws TException
      Copy all files in a directory to another directory (will overwrite)
      Parameters:
      sourceLocation - source for copy
      targetLocation - target for copy
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • updateDirectory

      public static void updateDirectory(File sourceLocation, File targetLocation) throws TException
      Copy all files in a directory to another directory (will not overwrite)
      Parameters:
      sourceLocation - source for copy
      targetLocation - target for copy
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • getDirectorySize

      public static long getDirectorySize(File directory)
      Return the size of all files in Directory
      Parameters:
      directory - starting directory for accumulating size
      Returns:
    • getDirectoryStats

      public static DirectoryStats getDirectoryStats(File directory)
      Return the count and size of all files in Directory
      Parameters:
      directory - start directory for tally
    • getDirectoryStats

      public static void getDirectoryStats(File directory, DirectoryStats stats)
      Return the count and size of all files in Directory
      Parameters:
      directory - start directory for tally
      stats - accumulating stats object
    • getDirectoryFiles

      public static void getDirectoryFiles(File sourceLocation, Vector<File> files) throws TException
      Build list of files for a directory
      Parameters:
      sourceLocation - start directory for extraction
      files - list of file contents
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • copy2Temp

      public static File copy2Temp(File sourceFile) throws TException
      Return a temp copy of a passed file
      Parameters:
      sourceFile - file to be copied
      Returns:
      copied form of sourceFile as temporary file
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • incFile

      public static void incFile(File incFile, int inc) throws TException
      Increment a stored file value and save back to file
      Parameters:
      incFile - file containing a numeric integer to be incremented
      inc - amount to increment
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • extractFileCnt

      public static long extractFileCnt(File extFile, long dflt) throws TException
      Parameters:
      extFile - file containing a numeric value
      dflt - default value if not found
      Returns:
      extracted value
      Throws:
      org.cdlib.mrt.utility.MException
      TException
    • deleteDir

      public static boolean deleteDir(File dir)
    • deleteEmptyPath

      public static void deleteEmptyPath(File dir)
      Delete up a directory path until a non-empty node is found Basically remove all empty directory levels above this directory.
      Parameters:
      dir - directory
    • deleteEmptyPath

      public static void deleteEmptyPath(File dir, File stopDir)
      Delete up a directory path until a non-empty node is found Basically remove all empty directory levels above this directory.
      Parameters:
      dir - directory
      stopDir - stop at this parent directory
    • populateTest

      public static void populateTest(File tempDir, String name, int lvl, int maxlvl) throws TException
       Create a test directory of form:
       A
       AA AB AC
       ...
       AAAA.txt AAAB.txt AAAC.txt
       Note content of .txt file is name of file
       
      Parameters:
      tempDir - this level directory
      lvl - level beginning with one indicating level in test tree
      maxlvl - maximum level count before file (not directories) are created
      Throws:
      TException
    • addTestFile

      protected static void addTestFile(File tempDir, String fileName) throws TException
      Add file at this directory level - ending with .txt Note that file content is name of file.
      Parameters:
      tempDir -
      fileName -
      Throws:
      TException
    • fileFromURL

      public static File fileFromURL(URL url)
      Get a file from a URL value - bug in 1.5
      Parameters:
      url - to be converted
      Returns:
      file
    • removeLineFromFile

      public static void removeLineFromFile(String file, String lineToRemove, String linePortion)
      remove line(s) from a text file
      Parameters:
      file - file to be processed
      lineToRemove - line to be removed
      linePortion - begins, ends contains or matches line (BEGIN, END, CONTAIN or MATCH, default: MATCH)
    • getLinesFromFile

      public static String[] getLinesFromFile(File splitFile) throws TException
      Return array of nonempty lines from a file
      Parameters:
      logger - merritt logger
      urlS - url to file to be split
      Returns:
      array of lines from original file
      Throws:
      TException
    • getLinesFromURL

      public static String[] getLinesFromURL(LoggerInf logger, String urlS) throws TException
      Return array of nonempty lines from url content
      Parameters:
      logger - merritt logger
      urlS - url to file to be split
      Returns:
      array of lines from original file
      Throws:
      TException
    • removeFile

      public static void removeFile(File inputFile)