Class FileTools

java.lang.Object
de.intarsys.tools.file.FileTools

public class FileTools extends Object
Some utility methods to ease life with File instances.
  • Field Details

  • Method Details

    • appendFile

      public static void appendFile(File source, File destination) throws IOException
      Concatenate the two files given in source and destination.
      Parameters:
      source - The file to be appended.
      destination - The file to append to.
      Throws:
      IOException
    • checkDirectory

      public static File checkDirectory(File dir, boolean create, boolean checkCanRead, boolean checkCanWrite) throws IOException
      Utility method for checking the availablity of a directory.
      Parameters:
      dir - The directory to check.
      create - Flag if we should create if dir not already exists.
      checkCanRead - Flag if we should check read permission.
      checkCanWrite - Flag if we should check write permission.
      Returns:
      The checked directory.
      Throws:
      IOException
    • checkDirectory

      public static File checkDirectory(String path, boolean create, boolean checkCanRead, boolean checkCanWrite) throws IOException
      Throws:
      IOException
      See Also:
    • copyBinaryFile

      public static void copyBinaryFile(File source, File destination) throws IOException
      Copy the byte content of source to destination.
      Parameters:
      source - The file whose contents we should copy.
      destination - The file where the contents are copied to.
      Throws:
      IOException
    • copyFile

      public static void copyFile(File source, File destination) throws IOException
      Throws:
      IOException
      See Also:
    • copyFile

      public static void copyFile(File source, String sourceEncoding, File destination, String destinationEncoding) throws IOException
      Copy the character content of source to destination.
      Parameters:
      source - The file whose contents we should copy.
      sourceEncoding - The encoding of the source byte stream.
      destination - The file where the contents are copied to.
      destinationEncoding - The encoding of the destination byte stream.
      Throws:
      IOException
    • copyRecursively

      public static void copyRecursively(File source, File destination) throws IOException
      Throws:
      IOException
    • copyRecursivelyInto

      public static File copyRecursivelyInto(File source, File destinationParent, String newName) throws IOException
      Throws:
      IOException
    • createEmptyFile

      public static void createEmptyFile(File file) throws IOException
      Create an empty file.
      Parameters:
      file -
      Throws:
      IOException
    • createTempFile

      public static File createTempFile(File file) throws IOException
      Create a file object representing a temporary file in the user's temp dir with the same name as the given file.
      Parameters:
      file - file to use
      Returns:
      file object representing a temporary file
      Throws:
      IOException
    • createTempFile

      public static File createTempFile(String filename) throws IOException
      Create a file object representing a temporary file in the user's temp dir with the given filename.

      This does not actually create a file in the file system.

      Parameters:
      filename - filename to use
      Returns:
      file object representing a temporary file
      Throws:
      IOException
    • deleteAfter

      public static void deleteAfter(File directory, long millis, boolean recursiveScan) throws IOException
      Delete any file in directory that is older than millis milliseconds. When recursiveScan is true the directory lookup is made recursive.
      Parameters:
      directory - The directory to scan.
      millis - The number of milliseconds a file is allowed to live.
      recursiveScan - Flag if we should handle directories recursive.
      Throws:
      IOException
    • deleteRecursivly

      public static boolean deleteRecursivly(File file)
      Deletes a file or directory, if necessary recursivly.

      Returns true if file could be deleted inclusive its components, otherwise false.

      Parameters:
      file - The file or directory to delete.
      Returns:
      true if file could be deleted inclusive its components, otherwise false.
    • deleteRecursivly

      public static boolean deleteRecursivly(File file, boolean deleteRoot)
      Deletes a file or directory, if necessary recursivly.

      Returns true if file could be deleted inclusive its components, otherwise false.

      Parameters:
      file - The file or directory to delete.
      deleteRoot - Flag if the root directory should be deleted itself.
      Returns:
      true if file could be deleted inclusive its components, otherwise false.
    • equalsOnSystem

      public static boolean equalsOnSystem(File source, File destination)
      true when the two files represent the same physical file in the file system.
      Parameters:
      source - The first file to be checked.
      destination - The second file to be checked.
      Returns:
      true when the two files represent the same physical file in the file system.
    • getBaseName

      public static String getBaseName(File file)
      Get the local name of the file in its directory without the extension.
      Parameters:
      file - The file whose base name is requested.
      Returns:
      The local name of the file in its directory without the extension.
    • getBaseName

      public static String getBaseName(File file, String defaultName)
      Get the local name of the file in its directory without the extension.
      Parameters:
      file - The file whose base name is requested.
      Returns:
      The local name of the file in its directory without the extension.
    • getBaseName

      public static String getBaseName(String filename)
      Get the local name of the file in its directory without the extension.
      Parameters:
      filename - The filename whose base name is requested.
      Returns:
      The local name of the file in its directory without the extension.
    • getBaseName

      public static String getBaseName(String filename, String defaultName)
      Get the local name of the file in its directory without the extension.
      Parameters:
      filename - The filename whose base name is requested.
      defaultName - returned if filename is null or a empty String
      Returns:
      The local name of the file in its directory without the extension.
    • getEncoding

      public static String getEncoding()
    • getExtension

      public static String getExtension(File file)
      Get the extension of the file name. If no extension is present, the empty string is returned.
      Parameters:
      file - The file whose extension is requested.
      Returns:
      The extension of the file name. If no extension is present, the empty string is returned.
    • getExtension

      public static String getExtension(String filename)
      Get the extension of the file name. If no extension is present, the empty string is returned.
      Parameters:
      filename - The filename whose extension is requested.
      Returns:
      The extension of the file name. If no extension is present, the empty string is returned.
    • getExtension

      public static String getExtension(String filename, String defaultName)
      Get the extension of the file name. If no extension is present, the defaultName is returned.
      Parameters:
      filename - The filename whose extension is requested.
      defaultName - returned if the filename is empty or null or there is no extension
      Returns:
      The extension of the file name. If no extension is present, the empty string is returned.
    • getFileName

      public static String getFileName(String filename)
      Get the local name of the file in its directory (with extension).
      Parameters:
      filename - The filename whose name is requested.
      Returns:
      The local name of the file in its directory (with extension)
    • getFileName

      public static String getFileName(String filename, String defaultName)
      Get the local name of the file in its directory (with extension).
      Parameters:
      filename - The filename whose name is requested.
      defaultName - returned if filename is null or a empty String
      Returns:
      The local name of the file in its directory (with extension)
    • getParentFile

      public static File getParentFile(File file)
      Try to get a valid parent for file.
      Parameters:
      file -
    • getPathRelativeTo

      public static String getPathRelativeTo(File file, File base) throws IOException
      get relative path of "file" with respect to "base" directory example : base = /a/b/c; file = /a/d/e/x.txt; getRelativePath(file, base) == ../../d/e/x.txt;
      Parameters:
      base - base path, should be a directory, not a file, or it doesn't make sense
      file - file to generate path for
      Returns:
      path from home to f as a string
      Throws:
      IOException
    • getPathRelativeTo

      public static String getPathRelativeTo(File file, File base, boolean ifAncestor)
    • isAncestor

      public static boolean isAncestor(File parent, File descendant)
    • isWindows

      public static boolean isWindows()
    • lock

      public static FileTools.Lock lock(File file)
    • renameFile

      public static void renameFile(File source, File destination) throws IOException
      Throws:
      IOException
      See Also:
    • renameFile

      public static void renameFile(File source, String sourceEncoding, File destination, String destinationEncoding) throws IOException
      "Rename" a file.

      The effect is that there is a new file destination, encoded in destinationEncoding, the old file source is deleted.

      Parameters:
      source - The source name of the file.
      sourceEncoding - The encoding of the source file.
      destination - The destination name of the file.
      destinationEncoding - The encoding of the destination file.
      Throws:
      IOException
    • resolvePath

      public static File resolvePath(File parent, String path)
      Return a new File instance for "path". If path is relative, than it will be interpreted as a child of "parent", if it is absolute, it is returned as is.

      ATTENTION: On windows, if "path" is absolute but without drive or UNC prefix, this root information is NOT taken from "parent".

      Parameters:
      parent -
      path -
      Returns:
      Return a new File instance for "path".
    • toBytes

      public static byte[] toBytes(File file) throws IOException
      Create a byte array with the files content.
      Parameters:
      file - The file to read.
      Returns:
      Create a byte array with the files content.
      Throws:
      IOException
    • toString

      public static String toString(File file) throws IOException
      Read a file's content at once and return as a string.

      Use with care!

      Parameters:
      file - The file to read.
      Returns:
      The string content of the file.
      Throws:
      IOException
    • toString

      public static String toString(File file, String encoding) throws IOException
      Read a file's content at once and return as a string in the correct encoding.

      Use with care!

      Parameters:
      file - The file to read.
      encoding - The encoding to use.
      Returns:
      The string content of the file.
      Throws:
      IOException
    • trimPath

      public static String trimPath(String param)
      Replaces all characters that are generally not allowed or considered useful in filenames with underscore.
      Parameters:
      param - java.lang.String
      Returns:
      java.lang.String
    • unlock

      protected static void unlock(FileTools.Lock lock)
    • wait

      public static void wait(File file, long timeout, long delay) throws IOException
      Wait for a file to arrive.

      The method waits at most timeout milliseconds for a file to arrive. When delay is != 0 the method checks the file's size for changes it reaches a stable size.

      Parameters:
      file - The file to wait for.
      timeout - The maximum time in milliseconds to wait for first occurence of file.
      delay - The number of milliseconds between two checks against the files size.
      Throws:
      IOException
    • write

      public static void write(File file, byte[] bytes) throws IOException
      Create a file from the byte content.
      Parameters:
      file - The file to write/create
      bytes - The data to be written into the file.
      Throws:
      IOException
    • write

      public static void write(File file, String text) throws IOException
      Create a file from the string content.
      Parameters:
      file - The file to write/create
      text - The text to be written into the file.
      Throws:
      IOException
    • write

      public static void write(File file, String text, boolean append) throws IOException
      Create a file from the string content.
      Parameters:
      file - The file to write/create
      text - The text to be written into the file.
      append - Flag to append to an existing file or create a new file.
      Throws:
      IOException
    • write

      public static void write(File file, String text, String encoding) throws IOException
      Create a file from the string content.
      Parameters:
      file - The file to write/create
      text - The text to be written into the file.
      Throws:
      IOException
    • write

      public static void write(File file, String text, String encoding, boolean append) throws IOException
      Create a file from the string content / append a string to a file
      Parameters:
      file - The file to write/create
      text - The text to be written into the file.
      append - Flag to append to an existing file or create a new file.
      Throws:
      IOException