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