Package gov.niem.tools.api.core.utils
Class FileUtils
java.lang.Object
gov.niem.tools.api.core.utils.FileUtils
File-related utilities to support the application.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Path
Creates a new directory at the given path.static File
createFile
(String path) Normalizes the given path and creates a new file at the location.static File
createFile
(String path, byte[] bytes) Normalizes the given path and creates a new file at the location with the given bytes.static Path
createTempDir
(String prefix) Creates a temporary directory.static File
createTempFile
(String prefix, String extension) Creates an empty temporary file.static Path
createTempFilePath
(String prefix, String extension) Creates a temporary file.static void
deleteTempDir
(File pathFile) Recursively deletes the given directory if it was created as a temporary directory by this utility.static void
deleteTempDir
(Path path) Recursively deletes the given directory if it was created as a temporary directory by this utility.static void
deleteTempFile
(File file) Deletes the given file if it was created as a temporary file by this utility.static void
deleteTempFile
(Path path) Deletes the file at the given path if it was created as a temporary file by this utility.static boolean
dirsFileCountsMatch
(Path dir1, Path dir2) Returns true if the two given paths contain the same number of files and subdirectories.static File
Returns a File object with the given path normalized.static boolean
filesMatch
(Path file1, Path file2) Returns true if the UTF-8 contents of file 1 match the contents of file 2.getDirPathsFromDir
(Path dir) Walks the given path to return a list of all directory paths.static File[]
getFileArrayFromDirWithExtension
(Path dir, String ext) Walks the given path to return an array of all files that match the given extension.static String
getFileExtension
(String filename) Returns the extension from the given filename.static String
getFileExtension
(Path filename) Returns the extension from the given filename.static String
getFileExtension
(org.springframework.web.multipart.MultipartFile multipartFile) Returns the extension from the given filename.static String
getFilename
(org.springframework.web.multipart.MultipartFile multipartFile) Gets the filename with extension.static String
getFilenameBase
(String filename) Gets the filename without the extension.static String
getFilenameBase
(org.springframework.web.multipart.MultipartFile multipartFile) Gets the filename without the extension.getFilePathsFromDir
(Path dir) Walks the given path to return a list of all file paths.getFilePathsFromDirWithExtension
(Path dir, String ext) Walks the given path to return a list of all file paths that match the given extension.getFilePathsFromDirWithFilename
(Path dir, String filename) Returns a list of file paths from the given directory that match the given filename.Converts the given list of path objects to a list of file objects.getFilesFromDirWithExtension
(Path dir, String ext) Walks the given path to return a list of all file that match the given extension.static String
getFileText
(Path path) Gets UTF-8 text from the file at the given path.static String
getFileText
(org.springframework.web.multipart.MultipartFile multipartFile) Gets UTF-8 text from the given multipart file.getPathsFromDir
(Path dir) Walks the given path to return a list of all file and directory paths.static void
moveTempDir
(File oldPath, File newPath) Moves the old directory to the new path if the old directory was created by this utility.static void
moveTempFile
(File oldFile, File newFile) Moves the temporary file to the new location if it was originally created by this utility.static void
moveTempFile
(Path oldFile, Path newFile) Moves the temporary file to the new location if it was originally created by this utility.static String
Returns the given Windows or Unix path normalized and formatted appropriately for the current system, with a trailing separator.static Path
Returns a Path object with the given path normalized.static void
Saves the given bytes to the given file path.static Path
saveFile
(org.springframework.web.multipart.MultipartFile multipartFile) Saves a multipart file to a temporary file.static Path
Saves a multipart file to the given path.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
getFilename
Gets the filename with extension. -
getFilenameBase
Gets the filename without the extension. Note: Treats ".cmf.xml" together as a double extension.- Parameters:
filename
- - Filename with an extension
-
getFilenameBase
Gets the filename without the extension.- Parameters:
multipartFile
- - Filename with an extension- Returns:
- Filename without the extension
-
getFileExtension
Returns the extension from the given filename. Note: Treats ".cmf.xml" as a double extension and returns both.- Parameters:
filename
- - Filename with extension
-
getFileExtension
Returns the extension from the given filename. Treats ".cmf.xml" as a double extension and returns both.- Parameters:
filename
- - Path to file with extension
-
getFileExtension
public static String getFileExtension(org.springframework.web.multipart.MultipartFile multipartFile) Returns the extension from the given filename. Note: Treats ".cmf.xml" as a double extension and returns both.- Parameters:
multipartFile
- - Uploaded input file
-
normalize
Returns the given Windows or Unix path normalized and formatted appropriately for the current system, with a trailing separator. -
file
Returns a File object with the given path normalized. -
path
Returns a Path object with the given path normalized. -
createFile
Normalizes the given path and creates a new file at the location.- Throws:
Exception
-
createFile
Normalizes the given path and creates a new file at the location with the given bytes.- Throws:
Exception
-
createTempFile
Creates an empty temporary file. Note: Use this utility to delete the temporary file when done.- Parameters:
prefix
- - Descriptive text for the file name that will precede the random number.- Throws:
Exception
-
createTempFilePath
Creates a temporary file. Note: Use this utility to delete the temporary file when done.- Parameters:
prefix
- - Descriptive text for the file name that will precede the random number.- Throws:
Exception
-
createDir
Creates a new directory at the given path.- Throws:
IOException
-
createTempDir
Creates a temporary directory. Note: Use this utility to delete the temporary directory when done.- Parameters:
prefix
- - Descriptive text for the directory name that will precede the random number.- Throws:
IOException
-
deleteTempFile
Deletes the given file if it was created as a temporary file by this utility.- Throws:
IOException
-
deleteTempFile
Deletes the file at the given path if it was created as a temporary file by this utility.- Throws:
IOException
-
deleteTempDir
Recursively deletes the given directory if it was created as a temporary directory by this utility.- Throws:
Exception
-
deleteTempDir
Recursively deletes the given directory if it was created as a temporary directory by this utility.- Throws:
Exception
-
moveTempFile
Moves the temporary file to the new location if it was originally created by this utility. Overwrites the destination file if it already exists.- Parameters:
oldFile
- - Original temporary file with contents.newFile
- - New destination for the file, to be overwritten if it already exists.- Throws:
Exception
-
moveTempFile
Moves the temporary file to the new location if it was originally created by this utility. Overwrites the destination file if it already exists.- Parameters:
oldFile
- - Original temporary file with contents.newFile
- - New destination for the file, to be overwritten if it already exists.- Throws:
Exception
-
moveTempDir
Moves the old directory to the new path if the old directory was created by this utility.- Parameters:
oldPath
- - Original temporary directory with contents.newPath
- - New destination for the original directory.- Throws:
Exception
-
saveFile
public static Path saveFile(org.springframework.web.multipart.MultipartFile multipartFile) throws Exception Saves a multipart file to a temporary file.- Throws:
Exception
-
saveFile
public static Path saveFile(org.springframework.web.multipart.MultipartFile multipartFile, Path dir) throws Exception Saves a multipart file to the given path.- Throws:
Exception
-
saveFile
Saves the given bytes to the given file path.- Throws:
IOException
-
getFileText
Gets UTF-8 text from the file at the given path.- Throws:
IOException
-
getFileText
public static String getFileText(org.springframework.web.multipart.MultipartFile multipartFile) throws IOException Gets UTF-8 text from the given multipart file.- Throws:
IOException
-
getPathsFromDir
Walks the given path to return a list of all file and directory paths.- Throws:
IOException
-
getFilePathsFromDir
Walks the given path to return a list of all file paths.- Throws:
IOException
-
getDirPathsFromDir
Walks the given path to return a list of all directory paths.- Throws:
IOException
-
getFilePathsFromDirWithExtension
Walks the given path to return a list of all file paths that match the given extension.- Throws:
IOException
-
getFilesFromDirWithExtension
Walks the given path to return a list of all file that match the given extension.- Throws:
IOException
-
getFileArrayFromDirWithExtension
Walks the given path to return an array of all files that match the given extension.- Throws:
IOException
-
getFiles
Converts the given list of path objects to a list of file objects. -
getFilePathsFromDirWithFilename
public static List<Path> getFilePathsFromDirWithFilename(Path dir, String filename) throws IOException Returns a list of file paths from the given directory that match the given filename.- Throws:
IOException
-
filesMatch
Returns true if the UTF-8 contents of file 1 match the contents of file 2.- Throws:
IOException
-
dirsFileCountsMatch
Returns true if the two given paths contain the same number of files and subdirectories.- Throws:
IOException
-