Class FileUtils


  • public class FileUtils
    extends Object
    Provides File utility methods.
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • formatFileSize

        public static String formatFileSize​(Number byteSize)
        Return a String notation of file size in MB or KB.
        Parameters:
        byteSize - the size of a file
        Returns:
        a String of file size
      • formatFileSizeKB

        public static String formatFileSizeKB​(Number byteSize)
        Return a String notation of file size notation in KB.
        Parameters:
        byteSize - the size of a file
        Returns:
        a String notation of file size
      • formatFileSizeMB

        public static String formatFileSizeMB​(Number byteSize)
        Return a String notation of file size notation in MB.
        Parameters:
        byteSize - the size of a file
        Returns:
        a String notation of file size
      • readTextFile

        public static List<String> readTextFile​(String fileName)
                                         throws IOException
        Return the file content and return a list of the lines of text in the file.
        Parameters:
        fileName - the file name to read
        Returns:
        An list of the lines of text in the file
        Throws:
        IOException - will be thrown if it can't read from the file
      • readTextFile

        public static List<String> readTextFile​(File file)
                                         throws IOException
        Return the file content and return a list of the lines of text in the file.
        Parameters:
        file - a file reference
        Returns:
        An list of the lines of text in the file
        Throws:
        IOException - will be thrown if it can't read from the file
      • writeToFile

        public static File writeToFile​(String filepath,
                                       byte[] byteArr)
                                throws FileNotFoundException,
                                       IOException
        Write the byte array to the file.
        Parameters:
        filepath - the file path to write
        byteArr - the byte array to write into the file
        Returns:
        the file reference
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • writeToFile

        public static void writeToFile​(File file,
                                       byte[] byteArr)
                                throws FileNotFoundException,
                                       IOException
        Write the byte array to the file.
        Parameters:
        file - the file reference
        byteArr - the byte array to write into the file
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • readFileAsString

        public static StringBuffer readFileAsString​(String filepath)
                                             throws FileNotFoundException,
                                                    IOException
        Read a file into a StringBuffer and return the StringBuffer reference.
        Parameters:
        filepath - the path of the file
        Returns:
        the string buffer of the file content
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • readFileAsString

        public static StringBuffer readFileAsString​(File file)
                                             throws FileNotFoundException,
                                                    IOException
        Read a file into a StringBuffer and return the StringBuffer reference.
        Parameters:
        file - the file reference
        Returns:
        the string buffer of the file content
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • readFileAsByteArray

        public static byte[] readFileAsByteArray​(File file)
                                          throws FileNotFoundException,
                                                 IOException
        Read a file into a byte array and return the byte array reference.
        Parameters:
        file - the file reference
        Returns:
        the byte array reference of the file content
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • moveFile

        public static boolean moveFile​(File sourceFile,
                                       File destinationFile)
                                throws FileNotFoundException,
                                       IOException
        This tries a file rename first then if that does not work it does a copy.
        Parameters:
        sourceFile - the source file reference to move
        destinationFile - the destination file reference for the file to move to
        Returns:
        true if the file is moved into the destination, otherwise the file is copied.
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • moveFileUsingCopy

        public static boolean moveFileUsingCopy​(File sourceFile,
                                                File destinationFile)
                                         throws FileNotFoundException,
                                                IOException
        This tries copy file first and then try to delete the source file.
        Parameters:
        sourceFile - the source file reference to move
        destinationFile - the destination file reference for the file to move to
        Returns:
        true
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • copyFileOld

        public static void copyFileOld​(File sourceFile,
                                       File destinationFile)
                                throws FileNotFoundException,
                                       IOException
        See copyFile for a faster version of this.
        Parameters:
        sourceFile - the source file reference to move
        destinationFile - the destination file reference for the file to move to
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • copyFile

        public static void copyFile​(File sourceFile,
                                    File destinationFile)
                             throws FileNotFoundException,
                                    IOException
        Copies one file to another.
        Parameters:
        sourceFile - the source file reference to move
        destinationFile - the destination file reference for the file to move to
        Throws:
        FileNotFoundException - will be thrown if file is not found
        IOException - will be thrown if it can't read from the file
      • checkDirectoryExists

        public static boolean checkDirectoryExists​(File fileToCheck)
        Check whether the directory exists.
        Parameters:
        fileToCheck - file reference to check
        Returns:
        true if the directory exists.
      • ensureDirectoryExists

        public static boolean ensureDirectoryExists​(String path)
        Create directory path if the directory does not exist.
        Parameters:
        path - directory path
        Returns:
        true if the directory was created, along with all necessary parent directories
      • createDateSubdirectory

        public static File createDateSubdirectory​(File directory)
                                           throws IOException
        Creates a date subdirectory under a given directory.
        Parameters:
        directory - a directory to add the date directory
        Returns:
        a file reference to the created date subdirectory
        Throws:
        IOException - will be thrown if it can't read from the file
      • fileNameSplit

        public static String[] fileNameSplit​(String fileName)
        Returns the file name and file extension.
        Parameters:
        fileName - the full file name e.g. notice.txt
        Returns:
        the file name and file extension
      • getFileExtension

        public static String getFileExtension​(String fileName)
        Return the file extension.
        Parameters:
        fileName - the full file name
        Returns:
        the file extension
      • changeFileExtension

        public static String changeFileExtension​(String fileName,
                                                 String newExtension)
        Change a file extension name.
        Parameters:
        fileName - full file name to be changed
        newExtension - the new extension to change
        Returns:
        the file name after change
      • getFileName

        public static String getFileName​(String fileNameWithExtension)
        Return the file name without extension.
        Parameters:
        fileNameWithExtension - the full file name
        Returns:
        the file name without extension
      • getOldestFile

        public static File getOldestFile​(File[] fileArray)
        Gets the oldest file in the list.
        Parameters:
        fileArray - containing a names of files (as returned by File.listFiles()
        Returns:
        the oldest file in the list
      • getNewestFile

        public static File getNewestFile​(File[] fileArray)
        Gets the newest file in the list.
        Parameters:
        fileArray - containing a names of files (as returned by File.listFiles()
        Returns:
        the newest file in the list
      • getSmallestFile

        public static File getSmallestFile​(File[] fileArray)
        Gets the smallest file in the list.
        Parameters:
        fileArray - containing a names of files (as returned by File.listFiles()
        Returns:
        the smallest File
      • getLargetsFile

        public static File getLargetsFile​(File[] fileArray)
        Gets the largest file in the list.
        Parameters:
        fileArray - containing a names of files (as returned by File.listFiles()
        Returns:
        the largest File
      • setLastModifiedTime

        public static boolean setLastModifiedTime​(File file,
                                                  Date dateTime)

        This method changes the modified time attribute for the given File. If the Date value is null then the method returns false and the modified time stays untouched.

        Parameters:
        file - - the file to which the modified time has to be changed
        dateTime - - The java.util.Date object to specify which is the time to use for change in modified time.
        Returns:
        - true if and only if the operation succeeded; false otherwise.
      • setLastModifiedTime

        public static boolean setLastModifiedTime​(File file)

        This method changes the modified time attribute for the given File. The method changes the modified time attribute with the current date-time.

        Parameters:
        file - - the file to which the modified time has to be changed
        Returns:
        - true if and only if the operation succeeded; false otherwise.
      • close

        public static void close​(InputStream stream)
        Close an input stream.
        Parameters:
        stream - an input stream
      • close

        public static void close​(ZipFile zipFile)
        Close a zipped file.
        Parameters:
        zipFile - a zipped file
      • close

        public static void close​(OutputStream stream)
        Close an output stream.
        Parameters:
        stream - an output stream
      • close

        public static void close​(Writer writer)
        Close a writer.
        Parameters:
        writer - a writer
      • close

        public static void close​(Reader reader)
        Close a reader.
        Parameters:
        reader - a reader
      • toByteArray

        public static byte[] toByteArray​(InputStream input)
                                  throws IOException
        Convert an input stream to a byte array
        Parameters:
        input - the input stream
        Returns:
        the converted byte array
        Throws:
        IOException - will be thrown if it can't read from the file
      • zip

        public static byte[] zip​(byte[] fileData,
                                 String fileName)
                          throws IOException
        Given a byte array and a file name, creates a ZIP file containing the input data as a single entry.
        Parameters:
        fileData - the byte array representing the ZIP entry (required)
        fileName - the name to use for the ZIP entry (required)
        Returns:
        a byte array representing the zipped file data
        Throws:
        IOException - if an error occurs zipping the data