Class FileUtils

java.lang.Object
nz.ac.waikato.cms.core.FileUtils

public class FileUtils extends Object
Utility class for I/O related actions.
Version:
$Revision: 10824 $
Author:
fracpete (fracpete at waikato dot ac dot nz)
  • Field Details

    • MAX_EXTENSION_LENGTH

      protected static Integer MAX_EXTENSION_LENGTH
      the maximum length for an extension.
    • IGNORED_EXTENSION_SUFFIXES

      protected static String[] IGNORED_EXTENSION_SUFFIXES
      the ignored extension suffixes.
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • replaceExtension

      public static File replaceExtension(File file, String newExt)
      Replaces the extension of the given file with the new one. Leave the new extension empty if you want to remove the extension. Always removes ignored extension suffixes first from the filename.
      Parameters:
      file - the file to replace the extension for
      newExt - the new extension (incl dot), empty string to remove extension
      Returns:
      the updated file
    • replaceExtension

      public static String replaceExtension(String file, String newExt)
      Replaces the extension of the given file with the new one. Leave the new extension empty if you want to remove the extension. Always removes ignored extension suffixes first from the filename.
      Parameters:
      file - the file to replace the extension for
      newExt - the new extension (incl dot), empty string to remove extension
      Returns:
      the updated file
    • getExtension

      public static String getExtension(File file)
      Returns the extension of the file, if any.
      Parameters:
      file - the file to get the extension from
      Returns:
      the extension (no dot), null if none available
    • getExtension

      public static String getExtension(String filename)
      Returns the extension of the file, if any.
      Parameters:
      filename - the file to get the extension from
      Returns:
      the extension (no dot), null if none available
    • getExtensions

      public static String[] getExtensions(File file)
      Returns the extensions of the file, if any. Returns "txt.gz" and "gz", for instance, for file "hello_world.txt.gz". The longer extension always comes first.
      Parameters:
      file - the file to get the extensions from
      Returns:
      the extensions (no dot), null if none available
    • getExtensions

      public static String[] getExtensions(String filename)
      Returns the extensions of the file, if any. Removes ignored file extension suffixes like "gz" first.
      Parameters:
      filename - the file to get the extensions from
      Returns:
      the extensions (no dot), null if none available
    • removeIgnoredExtensionSuffixes

      public static String removeIgnoredExtensionSuffixes(String filename)
      Removes any ignored extension suffixes from the filename.
      Parameters:
      filename - the filename to process
      Returns:
      the processed filename
    • closeQuietly

      public static void closeQuietly(InputStream is)
      Closes the stream, if possible, suppressing any exception.
      Parameters:
      is - the stream to close
    • closeQuietly

      public static void closeQuietly(OutputStream os)
      Closes the stream, if possible, suppressing any exception.
      Parameters:
      os - the stream to close
    • closeQuietly

      public static void closeQuietly(Reader reader)
      Closes the reader, if possible, suppressing any exception.
      Parameters:
      reader - the reader to close
    • closeQuietly

      public static void closeQuietly(Writer writer)
      Closes the writer, if possible, suppressing any exception.
      Parameters:
      writer - the writer to close
    • delete

      public static boolean delete(File file)
      Deletes the specified file. If the file represents a directory, then this will get deleted recursively.
      Parameters:
      file - the file/dir to delete
      Returns:
      true if successfully deleted
    • copyOrMove

      public static boolean copyOrMove(File sourceLocation, File targetLocation, boolean move, boolean atomic) throws IOException
      Copies or moves files and directories (recursively). If targetLocation does not exist, it will be created.

      Original code from Java-Tips.org.
      Parameters:
      sourceLocation - the source file/dir
      targetLocation - the target file/dir
      move - if true then the source files/dirs get deleted as soon as copying finished
      atomic - whether to perform an atomic move operation
      Returns:
      false if failed to delete when moving or failed to create target directory
      Throws:
      IOException - if copying/moving fails
    • writeToFileMsg

      public static String writeToFileMsg(String filename, Object obj, boolean append, String encoding)
      Writes the given object to the specified file. The message is either appended or replaces the current content of the file.
      Parameters:
      filename - the file to write to
      obj - the object to write
      append - whether to append the message or not
      encoding - the encoding to use, null for default
      Returns:
      true if writing was successful