Class FileUtil

java.lang.Object
com.pervasive.datarush.commons.util.FileUtil

public class FileUtil extends Object
Utility methods for operating on files.
  • Constructor Details

    • FileUtil

      public FileUtil()
  • Method Details

    • deleteDirectory

      public static boolean deleteDirectory(String dirPath, boolean removeRoot)
      Recursively deletes a directory structure starting at specified root directory.
      Parameters:
      dirPath - the path of the root directory
      removeRoot - if true the root directory is deleted after the deletion of its children
      Returns:
      true if all deletes succeeded, false otherwise
    • copy

      public static void copy(InputStream in, OutputStream out) throws IOException
      Copies the contents of the given input stream to the given output stream. Note that the caller is responsible for closing.
      Parameters:
      in - The input stream to read from.
      out - The output stream to write to.
      Throws:
      IOException - if I/O errors occur
    • deleteDirectory

      public static boolean deleteDirectory(File directory, boolean removeRoot)
      Recursively deletes a directory structure starting at specified root directory.
      Parameters:
      directory - the root directory
      removeRoot - if true the root directory is deleted after the deletion of its children
      Returns:
      true if all deletes succeeded, false otherwise
    • close

      public static <T extends InputStream> T close(T is) throws IOException
      Closes the given stream if not null
      Type Parameters:
      T - the type of stream
      Parameters:
      is - The stream
      Returns:
      always null. to be used in the pattern is=close(is)
      Throws:
      IOException - If an error occurs closing the stream.
    • close

      public static Socket close(Socket socket) throws IOException
      Closes the given socket if not null
      Parameters:
      socket - The socket
      Returns:
      always null. to be used in the pattern sock=close(sock)
      Throws:
      IOException - If an error occurs closing the socket.
    • close

      public static <T extends OutputStream> T close(T os) throws IOException
      Closes the given stream if not null
      Type Parameters:
      T - the type of stream
      Parameters:
      os - The stream
      Returns:
      always null. to be used in the pattern os=close(os)
      Throws:
      IOException - If an error occurs closing the stream.
    • close

      public static <T extends Closeable> T close(T close) throws IOException
      Closes the given stream if not null
      Type Parameters:
      T - the type of stream
      Parameters:
      close - The stream
      Returns:
      always null. to be used in the pattern os=close(os)
      Throws:
      IOException - If an error occurs closing the stream.
    • closeQuietly

      public static void closeQuietly(InputStream is)
      Closes the input stream if not null, suppressing any exception.
      Parameters:
      is - the input stream.
    • closeQuietly

      public static void closeQuietly(Socket socket)
      Closes the socket if not null, suppressing any exception.
      Parameters:
      socket - the socket.
    • closeQuietly

      public static void closeQuietly(Reader is)
      Closes the input stream if not null, suppressing any exception.
      Parameters:
      is - the input stream.
    • closeQuietly

      public static void closeQuietly(Closeable close)
      Closes the input stream if not null, suppressing any exception.
      Parameters:
      close - the input stream to close
    • closeQuietly

      public static void closeQuietly(OutputStream os)
      Closes the output stream if not null, suppressing any exception.
      Parameters:
      os - the output stream.
    • deleteRecursivelyWithFailure

      public static void deleteRecursivelyWithFailure(File dir) throws IOException
      Deletes the given directory or file, throwing an exception of unable to delete
      Parameters:
      dir - the directory to delete
      Throws:
      IOException - if unable to delete
    • deleteRecursively

      public static void deleteRecursively(File file)
      Recursively deletes the file and all subdirectories.
      Parameters:
      file - the file/directory to delete.
    • zipRecursively

      public static void zipRecursively(File from, File to) throws IOException
      Recursively zips the given input file/directory to the given file
      Parameters:
      from - may be a file or a directory
      to - must not be a directory
      Throws:
      IOException - if an error occurs
    • zipRecursively

      public static void zipRecursively(File from, ZipOutputStream zipOut) throws IOException
      Recursively zips the given input file/directory to the given zip stream
      Parameters:
      from - may be a file or a directory. if a directory, the zip will be rooted at the directory.
      zipOut - the zip output stream
      Throws:
      IOException - if an error occurs
    • unzip

      public static void unzip(File from, File to) throws IOException
      Recursively unzips the given file to a given directory/file
      Parameters:
      from - may be a file
      to - either directory or file
      Throws:
      IOException - if an error occurs
    • copyRecursively

      public static void copyRecursively(File from, File to) throws IOException
      Recursively copies the file/directory to the given file/directory.
      Parameters:
      from - the source file/directory
      to - the dest file/directory.
      Throws:
      IOException - if I/O errors occur
    • copyFile

      public static void copyFile(File from, File to) throws IOException
      Copies the file to the given location.
      Parameters:
      from - the source file ( must not be a directory )
      to - the dest file ( must not be a directory ).
      Throws:
      IOException - if an error occurs while copying the file
    • loadPropertiesFile

      public static Properties loadPropertiesFile(File file) throws IOException
      Loads the given properties file.
      Parameters:
      file - the file to load
      Returns:
      the file, parsed as a properties object.
      Throws:
      IOException - if I/O errors occur =
    • savePropertiesFile

      public static void savePropertiesFile(File file, Properties properties) throws IOException
      Saves the given properties object.
      Parameters:
      file - the file to save to.
      properties - the properties object to save.
      Throws:
      IOException - if I/O errors occur
    • readFileBytes

      public static byte[] readFileBytes(File file) throws IOException
      Reads the entire contents of the given file into a byte array.
      Parameters:
      file - the file to read.
      Returns:
      the contents of the file as bytes.
      Throws:
      IOException - if I/O errors occur
    • writeFileBytes

      public static void writeFileBytes(File file, byte[] bytes) throws IOException
      Write the entire contents of the given byte array to a file.
      Parameters:
      file - the file to write.
      bytes - the contents of the file as bytes
      Throws:
      IOException - if I/O errors occur
    • appendFileBytes

      public static void appendFileBytes(File file, byte[] bytes) throws IOException
      Appends the entire contents of the given byte array to a file.
      Parameters:
      file - the file to write.
      bytes - the contents of the file as bytes
      Throws:
      IOException - if I/O errors occur
    • readFileString

      public static String readFileString(File file, Charset charset) throws IOException
      Reads the entire contents of the given file into a string.
      Parameters:
      file - the file to read.
      charset - the character encoding of the file
      Returns:
      the contents of the file as a string
      Throws:
      IOException - if I/O errors occur
    • readZipEntry

      public static byte[] readZipEntry(File zipFile, String entry) throws IOException
      Reads an entry from a zip file
      Parameters:
      zipFile - the zip file
      entry - the name of the
      Returns:
      the contents of the entry or null if not found
      Throws:
      IOException
    • writeFileString

      public static void writeFileString(File file, String string, Charset charset) throws IOException
      Write the entire contents of the given string to a file.
      Parameters:
      file - the file to write.
      string - the contents of the file as a string
      charset - the character encoding for the file
      Throws:
      IOException - if I/O errors occur
    • writeData

      public static void writeData(String data, String fileName)
      Write data into a file.
      Parameters:
      data - data to be written into a file.
      fileName - the file name
      Throws:
      IOException - if I/O errors occur
    • readData

      public static String readData(File file)
      Read data from a file.
      Parameters:
      file - from which data to be read
      Returns:
      data read from the file
      Throws:
      IOException - if I/O errors occur
    • listFilesCanonical

      public static File[] listFilesCanonical(File directory)
      Lists the files in the given directory in a canonical ordering (one that is not subject to platform-specific ordering).
      Parameters:
      directory - the directory containing the files
      Returns:
      a list of file. this list will never be null but it may be empty
    • countLines

      public static int countLines(String outputFile)