Package gov.usgs.util
Class FileUtils
- java.lang.Object
-
- gov.usgs.util.FileUtils
-
public class FileUtils extends java.lang.Object
File input, output, content type, and delete utilities.
-
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
deleteEmptyParents(java.io.File path)
Delete path and any empty parent directories.static void
deleteEmptyParents(java.io.File path, java.io.File stopAt)
Delete path and any empty parent directories up to the stopAt point.static void
deleteTree(java.io.File path)
Delete all files recursively within tree.static void
extractResourceFile(java.lang.String rsFile, java.lang.String fsFile)
Extracts a resource file from within the executing JAR and copies it to the file system.static java.lang.String
getContentType(java.io.File file)
Get a file mime type based on its filename.static java.lang.String
getContentType(java.lang.String filename)
Get a file mime type based on its file path extension.static byte[]
readFile(java.io.File file)
Read file contents into a byte array.static void
writeFile(java.io.File file, byte[] content)
Write a file's content.static void
writeFileThenMove(java.io.File tempfile, java.io.File file, byte[] content)
Write a file's content atomically.
-
-
-
Method Detail
-
getContentType
public static java.lang.String getContentType(java.io.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 java.lang.String getContentType(java.lang.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(java.io.File file) throws java.io.IOException
Read file contents into a byte array.- Parameters:
file
- file to read.- Returns:
- byte array of file content.
- Throws:
java.io.IOException
- if an error occurs while reading.
-
writeFile
public static void writeFile(java.io.File file, byte[] content) throws java.io.IOException
Write a file's content.- Parameters:
file
- file to write.content
- content to write to file.- Throws:
java.io.IOException
- if any errors occur
-
writeFileThenMove
public static void writeFileThenMove(java.io.File tempfile, java.io.File file, byte[] content) throws java.io.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:
java.io.IOException
- if any errors occur.
-
deleteTree
public static void deleteTree(java.io.File path)
Delete all files recursively within tree.- Parameters:
path
- root of tree to delete.
-
deleteEmptyParents
public static void deleteEmptyParents(java.io.File path)
Delete path and any empty parent directories.- Parameters:
path
- directory to start in.
-
deleteEmptyParents
public static void deleteEmptyParents(java.io.File path, java.io.File stopAt)
Delete path and any empty parent directories up to the stopAt point.- Parameters:
path
- direcotry to start instopAt
- directory to stop at
-
extractResourceFile
public static void extractResourceFile(java.lang.String rsFile, java.lang.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.
-
-