Class FileUtils

java.lang.Object
gov.niem.tools.api.core.utils.FileUtils

public class FileUtils extends Object
File-related utilities to support the application.
  • Constructor Details

    • FileUtils

      public FileUtils()
  • Method Details

    • getFilename

      public static String getFilename(org.springframework.web.multipart.MultipartFile multipartFile)
      Gets the filename with extension.
    • getFilenameBase

      public static String getFilenameBase(String filename)
      Gets the filename without the extension. Note: Treats ".cmf.xml" together as a double extension.
      Parameters:
      filename - - Filename with an extension
    • getFilenameBase

      public static String getFilenameBase(org.springframework.web.multipart.MultipartFile multipartFile)
      Gets the filename without the extension.
      Parameters:
      multipartFile - - Filename with an extension
      Returns:
      Filename without the extension
    • getFileExtension

      public static String getFileExtension(String filename)
      Returns the extension from the given filename. Note: Treats ".cmf.xml" as a double extension and returns both.
      Parameters:
      filename - - Filename with extension
    • getFileExtension

      public static String getFileExtension(Path filename)
      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

      public static String normalize(String path)
      Returns the given Windows or Unix path normalized and formatted appropriately for the current system, with a trailing separator.
    • file

      public static File file(String path)
      Returns a File object with the given path normalized.
    • path

      public static Path path(String path)
      Returns a Path object with the given path normalized.
    • createFile

      public static File createFile(String path) throws Exception
      Normalizes the given path and creates a new file at the location.
      Throws:
      Exception
    • createFile

      public static File createFile(String path, byte[] bytes) throws Exception
      Normalizes the given path and creates a new file at the location with the given bytes.
      Throws:
      Exception
    • createTempFile

      public static File createTempFile(String prefix, String extension) throws Exception
      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

      public static Path createTempFilePath(String prefix, String extension) throws Exception
      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

      public static Path createDir(Path path) throws IOException
      Creates a new directory at the given path.
      Throws:
      IOException
    • createTempDir

      public static Path createTempDir(String prefix) throws IOException
      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

      public static void deleteTempFile(File file) throws IOException
      Deletes the given file if it was created as a temporary file by this utility.
      Throws:
      IOException
    • deleteTempFile

      public static void deleteTempFile(Path path) throws IOException
      Deletes the file at the given path if it was created as a temporary file by this utility.
      Throws:
      IOException
    • deleteTempDir

      public static void deleteTempDir(File pathFile) throws Exception
      Recursively deletes the given directory if it was created as a temporary directory by this utility.
      Throws:
      Exception
    • deleteTempDir

      public static void deleteTempDir(Path path) throws Exception
      Recursively deletes the given directory if it was created as a temporary directory by this utility.
      Throws:
      Exception
    • moveTempFile

      public static void moveTempFile(File oldFile, File newFile) throws Exception
      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

      public static void moveTempFile(Path oldFile, Path newFile) throws Exception
      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

      public static void moveTempDir(File oldPath, File newPath) throws Exception
      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

      public static void saveFile(Path path, byte[] bytes) throws IOException
      Saves the given bytes to the given file path.
      Throws:
      IOException
    • getFileText

      public static String getFileText(Path path) throws IOException
      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

      public static List<Path> getPathsFromDir(Path dir) throws IOException
      Walks the given path to return a list of all file and directory paths.
      Throws:
      IOException
    • getFilePathsFromDir

      public static List<Path> getFilePathsFromDir(Path dir) throws IOException
      Walks the given path to return a list of all file paths.
      Throws:
      IOException
    • getDirPathsFromDir

      public static List<Path> getDirPathsFromDir(Path dir) throws IOException
      Walks the given path to return a list of all directory paths.
      Throws:
      IOException
    • getFilePathsFromDirWithExtension

      public static List<Path> getFilePathsFromDirWithExtension(Path dir, String ext) throws IOException
      Walks the given path to return a list of all file paths that match the given extension.
      Throws:
      IOException
    • getFilesFromDirWithExtension

      public static List<File> getFilesFromDirWithExtension(Path dir, String ext) throws IOException
      Walks the given path to return a list of all file that match the given extension.
      Throws:
      IOException
    • getFileArrayFromDirWithExtension

      public static File[] getFileArrayFromDirWithExtension(Path dir, String ext) throws IOException
      Walks the given path to return an array of all files that match the given extension.
      Throws:
      IOException
    • getFiles

      public static List<File> getFiles(List<Path> paths)
      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

      public static boolean filesMatch(Path file1, Path file2) throws IOException
      Returns true if the UTF-8 contents of file 1 match the contents of file 2.
      Throws:
      IOException
    • dirsFileCountsMatch

      public static boolean dirsFileCountsMatch(Path dir1, Path dir2) throws IOException
      Returns true if the two given paths contain the same number of files and subdirectories.
      Throws:
      IOException