de.esprit.common.util

## Class FileUtil

- java.lang.Object
- - de.esprit.common.util.FileUtil

-     public class FileUtil
      extends java.lang.Object

  This is a utility class for dealing with files.
  Author:
    Rainer Buesch

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `DEFAULT_BUFF_SIZE`
    `static java.lang.String` | `DEFAULT_ENCODING`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.lang.String` | `baseNameOf(java.lang.String fileName)`
    `static java.io.File` | `buildDirectory(java.io.File rootDir, java.lang.String relativePath)` Null-safe construction of a file consisting of an absolute root directory, a relative basePath.
    `static java.io.File` | `buildFile(java.io.File rootDir, java.lang.String relativePath, java.lang.String fileName)` Null-safe construction of a file consisting of an absolute rootDir, a relativePath and a fileName.
    `static java.io.File` | `buildFile(java.lang.String relativePath)` Null-safe construction of a file from a relative path.
    `static java.io.File` | `buildFile(java.lang.String relativePath, java.lang.String fileName)` Null-safe construction of a file consisting of a relativePath and a fileName.
    `static void` | `checkExistsDirectory(java.io.File dir)`
    `static void` | `checkExistsFile(java.io.File file)`
    `static void` | `copyAll(java.io.File srcDir, java.io.File dstDir)` Copies the full directory recursively.
    `static void` | `copyAll(java.io.File srcDir, java.io.File dstDir, java.io.FileFilter filter)` Copies the full directory recursively and applies the given FileFilter
    `static float` | `copyAndUnzip(java.io.File zipInFile, java.io.File unzipOutFile)` Reads the zipped content of zipInFile and writes it unzipped to unzipOutFile.
    `static float` | `copyAndZip(java.io.File inFile, java.io.File zipOutFile)` Reads the content of inFile and writes it zipped to zipOutFile.
    `static int` | `copyAsciiResource(java.lang.String resourceName, java.io.File outFile)` Copies the given resource to the given File and returns the number of lines copied.
    `static int` | `copyAsciiResource(java.lang.String resourceName, java.io.File outFile, ReplacementMap map)` Same as `copyAsciiResource(String, File)` but additionally performs the replacements defined `ReplacementMap` in the in all lines being copied.
    `static long` | `copyAsciiStream(java.io.InputStream in, java.io.File outFile)` Copies the ASCII content of from a stream into a file.
    `static long` | `copyAsciiStream(java.io.InputStream in, java.io.OutputStream out)` Copies the ASCII content of one stream to another one.
    `static long` | `copyBinaryResource(java.lang.String resourceName, java.io.File outFile)`
    `static long` | `copyBinaryStream(java.io.InputStream in, java.io.File outFile)` Copies the BINARY content of from a stream into a file.
    `static long` | `copyBinaryStream(java.io.InputStream in, java.io.OutputStream out)` Copies the BINARY content of one stream to another one.
    `static void` | `copyFile(java.io.File fileIn, java.io.File fileOut)` Copies the content of one file to another.
    `static long` | `copyStream(java.io.InputStream in, java.io.OutputStream out)` Copies the content of one stream to another one using an internal buffSize of DEFAULT_BUFF_SIZE.
    `static long` | `copyStream(java.io.InputStream in, java.io.OutputStream out, int buffSize)` Copies the content of one stream to another one with the specified buffSize.
    `static int` | `countLines(java.io.File file)` Skips through the file content and counts the number of lines in it.
    `static java.lang.String` | `cutExtensionFrom(java.lang.String fileName)`
    `static void` | `deleteDir(java.io.File dir)` Removes all files within the given directory and then deletes the directory itself.
    `static void` | `deleteDir(java.io.File dir, boolean isRecursive)` Removes all files within the given directory and then deletes the directory itself.
    `static boolean` | `deleteFile(java.io.File file)` Removes the given file/directory and throws an Exception if the deletion failed
    `static int` | `deleteFiles(java.io.File dir)` Removes silently all files within the given directory and returns the number of files that could be deleted successfully.
    `static java.lang.String` | `getFileBaseName(java.lang.String fileName)` Cuts off the extension of a filename if it has any
    `static java.lang.String` | `getFileTs()` Returns a timestamp string as used for filename creation.
    `static java.io.InputStream` | `getResourceAsStream(java.lang.String resourceName)` Tries to open an InputStream to the given resource file.
    `static java.lang.String` | `guessMimeTypeFor(java.io.File file)` Tries to guess the mime-type string of a file.
    `static java.io.File[]` | `listFiles(java.io.File dir)` Get listing of dir always suitable for iteration over array in for-loops
    `static java.io.File[]` | `listFiles(java.io.File dir, java.io.FileFilter filter)` Get listing of dir always suitable for iteration over array in for-loops
    `static java.io.File[]` | `listFiles(java.io.File dir, java.io.FilenameFilter filter)` Get listing of dir always suitable for iteration over array in for-loops
    `static boolean` | `mkdir(java.io.File dir)`
    `static boolean` | `mkdir(java.lang.String dirName)`
    `static java.io.File` | `mkdirs(java.io.File dir)` Creates the full directory tree.
    `static java.io.File` | `mkdirs(java.lang.String dirName)` Creates the directory tree.
    `static void` | `purgeDir(java.io.File dir)` Removes the whole content of a directory not removing the directory itself.
    `static byte[]` | `readBinaryData(java.io.InputStream inStream)` Reads the content of the given stream into a byte array
    `static byte[]` | `readBinaryFile(java.io.File file)` Reads the content of the given file into a byte array
    `static byte[]` | `readBinaryFile(java.lang.String fileName)` Reads the content of the given file into a byte array
    `static java.util.List<java.io.File>` | `readDirectoryTree(java.io.File rootDir)` Reads all directories below rootDir recursively
    `static java.util.List<java.io.File>` | `readFileTree(java.io.File rootDir, java.io.FileFilter filter)` Reads all files under rootDir and its subdirectories recursively.
    `static java.lang.String` | `readText(java.io.InputStream inStream)`
    `static java.lang.String` | `readText(java.io.InputStream inStream, int maxLines)`
    `static java.lang.String` | `readText(java.io.InputStream inStream, java.lang.String encoding)`
    `static java.lang.String` | `readText(java.io.InputStream inStream, java.lang.String encoding, int maxLines)` Returns the text read from the file.
    `static java.lang.String` | `readTextFile(java.io.File file)` Reads the content of the given file into a String using UNICODE encoding
    `static java.lang.String` | `readTextFile(java.io.File file, java.lang.String encoding)` Reads the content of the given file into a String using the given encoding.
    `static java.lang.String` | `readTextFile(java.lang.String fileName)` Reads the content of the given file into a String
    `static java.lang.String` | `readTextResource(java.lang.String resourceName)` Reads the content of the given file into a String
    `static java.io.File` | `resolvePath(char varIdentifier, java.lang.String path)`
    `static java.io.File` | `resolvePath(java.lang.String path)`
    `static void` | `setLastModified(java.io.File file, long time)` Updates the modification timestamp on a File.
    `static java.lang.String` | `toRelativePath(java.io.File rootPath, java.io.File absolutePath)`
    `static void` | `touch(java.io.File file)` Updates the modification timestamp on a File.
    `static void` | `touch(java.io.File file, long time)` Updates the modification timestamp on a File.
    `static java.lang.String` | `toUnixPathName(java.io.File file)` Converts all backslashes to slashes in a file-pathname
    `static java.lang.String` | `toUnixPathName(java.lang.String p)` Converts all backslashes to slashes in a file-pathname
    `static java.lang.String` | `toValidFileName(java.lang.String str)` Converts the String to a valid file name.
    `static boolean` | `truncate(java.io.File file)` Clears the content of the given File
    `static <E> E` | `unzipObject(byte[] data)` Deserializes and instantiates an Object from data in GZIP format.
    `static void` | `writeBinaryFile(java.io.File file, byte[] data)` Writes the content of the given byte array to the file
    `static void` | `writeBinaryFile(java.lang.String fileName, byte[] data)` Writes the content of the given byte array to the file
    `static int` | `writeText(java.io.OutputStream outStream, java.lang.String text)` Writes the given text (may be multi-line) to the outStream with the default platform encoding.
    `static int` | `writeText(java.io.OutputStream outStream, java.lang.String text, java.lang.String encoding)` Writes the given text (may be multi-line) to the outStream with the given encoding.
    `static int` | `writeTextFile(java.io.File file, java.lang.String text)` Writes the given String into the given File using UNICODE encoding
    `static int` | `writeTextFile(java.io.File file, java.lang.String text, java.lang.String encoding)` Writes the given String into the given File using the given encoding.
    `static byte[]` | `zipObject(java.lang.Object arg)` Serializes the Object and zips the data in GZIP format.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_ENCODING

          public static final java.lang.String DEFAULT_ENCODING

      See Also:
        Constant Field Values

    - #### DEFAULT_BUFF_SIZE

          public static final int DEFAULT_BUFF_SIZE

      See Also:
        Constant Field Values

  - ### Method Detail

    - #### buildFile

          public static java.io.File buildFile(java.io.File rootDir,
                               java.lang.String relativePath,
                               java.lang.String fileName)

      Null-safe construction of a file consisting of an absolute rootDir, a relativePath and a fileName. Note that fileName must not be null.

    - #### baseNameOf

          public static java.lang.String baseNameOf(java.lang.String fileName)

    - #### buildFile

          public static java.io.File buildFile(java.lang.String relativePath,
                               java.lang.String fileName)

      Null-safe construction of a file consisting of a relativePath and a fileName. Note that fileName must not be null.

    - #### buildFile

          public static java.io.File buildFile(java.lang.String relativePath)

      Null-safe construction of a file from a relative path. If the path is null, then the '.' directory is returned.

    - #### buildDirectory

          public static java.io.File buildDirectory(java.io.File rootDir,
                                    java.lang.String relativePath)

      Null-safe construction of a file consisting of an absolute root directory, a relative basePath.

    - #### cutExtensionFrom

          public static java.lang.String cutExtensionFrom(java.lang.String fileName)

    - #### getResourceAsStream

          public static java.io.InputStream getResourceAsStream(java.lang.String resourceName)
                                                         throws java.io.IOException

      Tries to open an InputStream to the given resource file.
      Throws:
        `java.io.IOException`

    - #### copyAsciiResource

          public static int copyAsciiResource(java.lang.String resourceName,
                              java.io.File outFile)
                                       throws java.io.IOException

      Copies the given resource to the given File and returns the number of lines copied. Note that the number of bytes may differ in source and target due to different line separators on different platforms.
      Returns:
        number of lines copied
      Throws:
        `java.io.IOException`

    - #### copyAsciiResource

          public static int copyAsciiResource(java.lang.String resourceName,
                              java.io.File outFile,
                              ReplacementMap map)
                                       throws java.io.IOException

      Same as `copyAsciiResource(String, File)` but additionally performs the replacements defined `ReplacementMap` in the in all lines being copied.
      Returns:
        number of lines copied
      Throws:
        `java.io.IOException`

    - #### copyBinaryResource

          public static long copyBinaryResource(java.lang.String resourceName,
                                java.io.File outFile)
                                         throws java.io.IOException

      Returns:
        number of bytes copied
      Throws:
        `java.io.IOException`

    - #### readBinaryFile

          public static byte[] readBinaryFile(java.lang.String fileName)
                                       throws java.io.IOException

      Reads the content of the given file into a byte array
      Throws:
        `java.io.IOException`

    - #### readBinaryFile

          public static byte[] readBinaryFile(java.io.File file)
                                       throws java.io.IOException

      Reads the content of the given file into a byte array
      Throws:
        `java.io.IOException`

    - #### readBinaryData

          public static byte[] readBinaryData(java.io.InputStream inStream)
                                       throws java.io.IOException

      Reads the content of the given stream into a byte array
      Throws:
        `java.io.IOException`

    - #### writeBinaryFile

          public static void writeBinaryFile(java.lang.String fileName,
                             byte[] data)
                                      throws java.io.IOException

      Writes the content of the given byte array to the file
      Throws:
        `java.io.IOException`

    - #### writeBinaryFile

          public static void writeBinaryFile(java.io.File file,
                             byte[] data)
                                      throws java.io.IOException

      Writes the content of the given byte array to the file
      Throws:
        `java.io.IOException`

    - #### readTextFile

          public static java.lang.String readTextFile(java.lang.String fileName)
                                               throws java.io.IOException

      Reads the content of the given file into a String
      Throws:
        `java.io.IOException`

    - #### readTextFile

          public static java.lang.String readTextFile(java.io.File file)
                                               throws java.io.IOException

      Reads the content of the given file into a String using UNICODE encoding
      Throws:
        `java.io.IOException`

    - #### readTextFile

          public static java.lang.String readTextFile(java.io.File file,
                                      java.lang.String encoding)
                                               throws java.io.IOException

      Reads the content of the given file into a String using the given encoding. If the encoding is null then the platform specific default encoding is used.
      Throws:
        `java.io.IOException`

    - #### readTextResource

          public static java.lang.String readTextResource(java.lang.String resourceName)
                                                   throws java.io.IOException

      Reads the content of the given file into a String
      Throws:
        `java.io.IOException`

    - #### readText

          public static java.lang.String readText(java.io.InputStream inStream)
                                           throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### readText

          public static java.lang.String readText(java.io.InputStream inStream,
                                  int maxLines)
                                           throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### readText

          public static java.lang.String readText(java.io.InputStream inStream,
                                  java.lang.String encoding)
                                           throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### readText

          public static java.lang.String readText(java.io.InputStream inStream,
                                  java.lang.String encoding,
                                  int maxLines)
                                           throws java.io.IOException

      Returns the text read from the file. If maxLines > 0 then the lines are truncated accordingly.
      Throws:
        `java.io.IOException`

    - #### countLines

          public static int countLines(java.io.File file)
                                throws java.io.IOException

      Skips through the file content and counts the number of lines in it.
      Throws:
        `java.io.IOException`

    - #### writeTextFile

          public static int writeTextFile(java.io.File file,
                          java.lang.String text)
                                   throws java.io.IOException

      Writes the given String into the given File using UNICODE encoding
      Returns:
        the number of lines written.
      Throws:
        `java.io.IOException`

    - #### writeTextFile

          public static int writeTextFile(java.io.File file,
                          java.lang.String text,
                          java.lang.String encoding)
                                   throws java.io.IOException

      Writes the given String into the given File using the given encoding. If the encoding is null then the platform specific default encoding is used.
      Returns:
        the number of lines written.
      Throws:
        `java.io.IOException`

    - #### writeText

          public static int writeText(java.io.OutputStream outStream,
                      java.lang.String text)
                               throws java.io.IOException

      Writes the given text (may be multi-line) to the outStream with the default platform encoding.
      Returns:
        the number of lines written.
      Throws:
        `java.io.IOException`

    - #### writeText

          public static int writeText(java.io.OutputStream outStream,
                      java.lang.String text,
                      java.lang.String encoding)
                               throws java.io.IOException

      Writes the given text (may be multi-line) to the outStream with the given encoding.
      Returns:
        the number of lines written.
      Throws:
        `java.io.IOException`

    - #### getFileTs

          public static java.lang.String getFileTs()

      Returns a timestamp string as used for filename creation.

    - #### getFileBaseName

          public static java.lang.String getFileBaseName(java.lang.String fileName)

      Cuts off the extension of a filename if it has any

    - #### toValidFileName

          public static java.lang.String toValidFileName(java.lang.String str)

      Converts the String to a valid file name. All white spaces are replaced by '_'

    - #### toUnixPathName

          public static java.lang.String toUnixPathName(java.lang.String p)

      Converts all backslashes to slashes in a file-pathname

    - #### toUnixPathName

          public static java.lang.String toUnixPathName(java.io.File file)

      Converts all backslashes to slashes in a file-pathname

    - #### toRelativePath

          public static java.lang.String toRelativePath(java.io.File rootPath,
                                        java.io.File absolutePath)

    - #### copyFile

          public static void copyFile(java.io.File fileIn,
                      java.io.File fileOut)
                               throws java.io.IOException

      Copies the content of one file to another.
      Throws:
        `java.io.IOException`

    - #### copyBinaryStream

          public static long copyBinaryStream(java.io.InputStream in,
                              java.io.File outFile)
                                       throws java.io.IOException

      Copies the BINARY content of from a stream into a file.
      Returns:
        the total number of bytes copied
      Throws:
        `java.io.IOException`

    - #### copyBinaryStream

          public static long copyBinaryStream(java.io.InputStream in,
                              java.io.OutputStream out)
                                       throws java.io.IOException

      Copies the BINARY content of one stream to another one.
      Returns:
        the total number of bytes copied
      Throws:
        `java.io.IOException`

    - #### copyAsciiStream

          public static long copyAsciiStream(java.io.InputStream in,
                             java.io.File outFile)
                                      throws java.io.IOException

      Copies the ASCII content of from a stream into a file.
      Returns:
        the total number of bytes copied
      Throws:
        `java.io.IOException`

    - #### copyAsciiStream

          public static long copyAsciiStream(java.io.InputStream in,
                             java.io.OutputStream out)
                                      throws java.io.IOException

      Copies the ASCII content of one stream to another one.
      Returns:
        the total number of bytes copied
      Throws:
        `java.io.IOException`

    - #### copyStream

          public static long copyStream(java.io.InputStream in,
                        java.io.OutputStream out)
                                 throws java.io.IOException

      Copies the content of one stream to another one using an internal buffSize of DEFAULT_BUFF_SIZE.
      Returns:
        the total number of bytes copied
      Throws:
        `java.io.IOException`

    - #### copyStream

          public static long copyStream(java.io.InputStream in,
                        java.io.OutputStream out,
                        int buffSize)
                                 throws java.io.IOException

      Copies the content of one stream to another one with the specified buffSize.
      Returns:
        the total number of bytes copied
      Throws:
        `java.io.IOException`

    - #### copyAndZip

          public static float copyAndZip(java.io.File inFile,
                         java.io.File zipOutFile)
                                  throws java.io.IOException

      Reads the content of inFile and writes it zipped to zipOutFile.
      Returns:
        the compression factor
      Throws:
        `java.io.IOException`
      See Also:
        `copyAndUnzip(File, File)`

    - #### copyAndUnzip

          public static float copyAndUnzip(java.io.File zipInFile,
                           java.io.File unzipOutFile)
                                    throws java.io.IOException

      Reads the zipped content of zipInFile and writes it unzipped to unzipOutFile.
      Returns:
        the decompression factor
      Throws:
        `java.io.IOException`
      See Also:
        `copyAndZip(File, File)`

    - #### zipObject

          public static byte[] zipObject(java.lang.Object arg)

      Serializes the Object and zips the data in GZIP format.
      See Also:
        `unzipObject(byte[])`

    - #### unzipObject

          public static <E> E unzipObject(byte[] data)
                               throws java.lang.ClassNotFoundException

      Deserializes and instantiates an Object from data in GZIP format.
      Throws:
        `java.lang.ClassNotFoundException`
      See Also:
        `zipObject(Object)`

    - #### readDirectoryTree

          public static java.util.List<java.io.File> readDirectoryTree(java.io.File rootDir)

      Reads all directories below rootDir recursively

    - #### readFileTree

          public static java.util.List<java.io.File> readFileTree(java.io.File rootDir,
                                                  java.io.FileFilter filter)

      Reads all files under rootDir and its subdirectories recursively. If filter is not null, all Files will match the filter.

    - #### mkdir

          public static boolean mkdir(java.lang.String dirName)
                               throws java.lang.Exception

      Returns:
        true if the directory was newly created
      Throws:
        `java.lang.Exception`

    - #### mkdir

          public static boolean mkdir(java.io.File dir)
                               throws java.lang.Exception

      Returns:
        true if the directory was newly created
      Throws:
        `java.lang.Exception`

    - #### mkdirs

          public static java.io.File mkdirs(java.lang.String dirName)
                                     throws java.lang.Exception

      Creates the directory tree.
      Returns:
        the for sure existing directory.
      Throws:
        `java.lang.Exception`

    - #### mkdirs

          public static java.io.File mkdirs(java.io.File dir)
                                     throws java.lang.Exception

      Creates the full directory tree.
      Returns:
        the for sure existing directory.
      Throws:
        `java.lang.Exception`

    - #### purgeDir

          public static void purgeDir(java.io.File dir)
                               throws java.lang.Exception

      Removes the whole content of a directory not removing the directory itself.
      Throws:
        `java.lang.Exception`

    - #### deleteDir

          public static void deleteDir(java.io.File dir,
                       boolean isRecursive)
                                throws java.lang.Exception

      Removes all files within the given directory and then deletes the directory itself.
      Parameters:
        `isRecursive` - delete the whole directory recursively
      Throws:
        `java.lang.Exception`

    - #### deleteFiles

          public static int deleteFiles(java.io.File dir)

      Removes silently all files within the given directory and returns the number of files that could be deleted successfully.

    - #### deleteDir

          public static void deleteDir(java.io.File dir)
                                throws java.lang.Exception

      Removes all files within the given directory and then deletes the directory itself. Does NOT delete recursively!!
      Throws:
        `java.lang.Exception`

    - #### deleteFile

          public static boolean deleteFile(java.io.File file)
                                    throws java.lang.Exception

      Removes the given file/directory and throws an Exception if the deletion failed
      Throws:
        `java.lang.Exception`

    - #### checkExistsFile

          public static void checkExistsFile(java.io.File file)
                                      throws java.lang.Exception

      Throws:
        `java.lang.Exception`

    - #### checkExistsDirectory

          public static void checkExistsDirectory(java.io.File dir)
                                           throws java.lang.Exception

      Throws:
        `java.lang.Exception`

    - #### listFiles

          public static java.io.File[] listFiles(java.io.File dir)

      Get listing of dir always suitable for iteration over array in for-loops
      Parameters:
        `dir` -
      Returns:
        always a File[]

    - #### listFiles

          public static java.io.File[] listFiles(java.io.File dir,
                                 java.io.FileFilter filter)

      Get listing of dir always suitable for iteration over array in for-loops
      Parameters:
        `dir` -
        `filter` -
      Returns:
        always a File[]

    - #### listFiles

          public static java.io.File[] listFiles(java.io.File dir,
                                 java.io.FilenameFilter filter)

      Get listing of dir always suitable for iteration over array in for-loops
      Parameters:
        `dir` -
        `filter` -
      Returns:
        always a File[]

    - #### truncate

          public static boolean truncate(java.io.File file)
                                  throws java.lang.Exception

      Clears the content of the given File
      Throws:
        `java.lang.Exception`

    - #### copyAll

          public static void copyAll(java.io.File srcDir,
                     java.io.File dstDir)
                              throws java.lang.Exception

      Copies the full directory recursively.
      Throws:
        `java.lang.Exception`

    - #### copyAll

          public static void copyAll(java.io.File srcDir,
                     java.io.File dstDir,
                     java.io.FileFilter filter)
                              throws java.lang.Exception

      Copies the full directory recursively and applies the given FileFilter
      Throws:
        `java.lang.Exception`

    - #### touch

          public static void touch(java.io.File file)
                            throws java.lang.Exception

      Updates the modification timestamp on a File. The file is created, if it does not exist yet.
      Throws:
        `java.lang.Exception`

    - #### touch

          public static void touch(java.io.File file,
                   long time)
                            throws java.lang.Exception

      Updates the modification timestamp on a File. The file is created, if it does not exist yet.
      Throws:
        `java.lang.Exception`

    - #### setLastModified

          public static void setLastModified(java.io.File file,
                             long time)
                                      throws java.lang.Exception

      Updates the modification timestamp on a File.
      Throws:
        `java.lang.Exception` - if the file does not exist.

    - #### guessMimeTypeFor

          public static java.lang.String guessMimeTypeFor(java.io.File file)
                                                   throws java.io.IOException

      Tries to guess the mime-type string of a file.
      Throws:
        `java.io.IOException`

    - #### resolvePath

          public static java.io.File resolvePath(java.lang.String path)

    - #### resolvePath

          public static java.io.File resolvePath(char varIdentifier,
                                 java.lang.String path)

Copyright © 2014 EsprIT-Systems. All Rights Reserved.