Package gov.usgs.util

Class FileUtils

java.lang.Object
gov.usgs.util.FileUtils

public class FileUtils extends Object
File input, output, content type, and delete utilities.
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • getContentType

      public static String getContentType(File file)
      Get a file mime type based on its filename. Calls getContentType(file.getName()).
      Parameters:
      file - a file to get type from
      Returns:
      String mime type.
    • getContentType

      public static String getContentType(String filename)
      Get a file mime type based on its file path extension. Uses URLConnection.getFileNameMap().
      Parameters:
      filename - file path.
      Returns:
      String mime type.
    • readFile

      public static byte[] readFile(File file) throws IOException
      Read file contents into a byte array.
      Parameters:
      file - file to read.
      Returns:
      byte array of file content.
      Throws:
      IOException - if an error occurs while reading.
    • writeFile

      public static void writeFile(File file, byte[] content) throws IOException
      Write a file's content.
      Parameters:
      file - file to write.
      content - content to write to file.
      Throws:
      IOException - if any errors occur
    • writeFileThenMove

      public static void writeFileThenMove(File tempfile, File file, byte[] content) throws IOException
      Write a file's content atomically.
      Parameters:
      tempfile - where file is written.
      file - where tempfile is moved after writing.
      content - file content to write.
      Throws:
      IOException - if any errors occur.
    • deleteTree

      public static void deleteTree(File path)
      Delete all files recursively within tree.
      Parameters:
      path - root of tree to delete.
    • deleteEmptyParents

      public static void deleteEmptyParents(File path)
      Delete path and any empty parent directories.
      Parameters:
      path - directory to start in.
    • deleteEmptyParents

      public static void deleteEmptyParents(File path, File stopAt)
      Delete path and any empty parent directories up to the stopAt point.
      Parameters:
      path - direcotry to start in
      stopAt - directory to stop at
    • extractResourceFile

      public static void extractResourceFile(String rsFile, String fsFile)
      Extracts a resource file from within the executing JAR and copies it to the file system.
      Parameters:
      rsFile - Absolute file path (relative to JAR root) of the file to extract.
      fsFile - File path where to place the extracted file. An absolute path here is relative to the file system root. Relative paths for this value are relative to the CWD.