Class FileUtils


  • public class FileUtils
    extends Object
    This class defines the File Utils to be used in MOSIP Project The File Utils are implemented using methods of org.apache.commons.io.FileUtils class of Apache commons.io package
    Since:
    1.0.0
    Author:
    Priya Soni
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String byteCountToDisplaySize​(long size)
      Returns a human-readable version of the file size, where the input represents a specific number of bytes.
      static Checksum checksum​(File file, Checksum checksum)
      Computes the checksum of a file using the specified checksum object
      static long checksumCRC32​(File file)
      Computes the checksum of a file using the CRC32 checksum routine
      static void cleanDirectory​(File directory)
      Cleans a directory without deleting it
      static boolean contentEquals​(File file1, File file2)
      Compares the contents of two files to determine if they are equal or not
      static boolean contentEqualsIgnoreEOL​(File file1, File file2, String charsetName)
      Compares the contents of two files to determine if they are equal or not.
      static File[] convertFileCollectionToFileArray​(Collection<File> files)
      Converts a Collection containing java.io.File instanced into array representation
      static void copyDirectory​(File srcDir, File destDir)
      Copies a whole directory to a new location preserving the file dates
      static void copyFile​(File srcFile, File destFile)
      Copies a file to a new location preserving the file date
      static long copyFile​(File input, OutputStream output)
      Copy bytes from a File to an OutputStream.
      static void copyInputStreamToFile​(InputStream source, File destination)
      Copies bytes from an InputStream source to a file destination.
      static void copyToFile​(InputStream source, File destination)
      Copies bytes from an InputStream source to a file destination The source stream is left open
      static void deleteDirectory​(File directory)
      Deletes a directory recursively
      static boolean deleteQuietly​(File file)
      Deletes a file, never throwing an exception
      static boolean directoryContains​(File directory, File child)
      Determines whether the parent directory contains the child element (a file or directory)
      static void forceDelete​(File file)
      Deletes a file.
      static void forceDeleteOnExit​(File file)
      Schedules a file to be deleted when JVM exits
      static File getFile​(File directory, String... names)
      Construct a file from the set of name elements.
      static File getFile​(String... names)
      Construct a file from the set of name elements.
      static boolean isFileNewer​(File file, Date date)
      Tests if the specified File is newer than the specified Date
      static boolean isFileOlder​(File file, Date date)
      Tests if the specified File is older than the specified Date
      static boolean isSymlink​(File file)
      Determines whether the specified file is a Symbolic Link rather than an actual file
      static Iterator<File> iterateFiles​(File directory, org.apache.commons.io.filefilter.IOFileFilter fileFilter, org.apache.commons.io.filefilter.IOFileFilter dirFilter)
      Allows iteration over the files in given directory (and optionally its sub directories).
      static org.apache.commons.io.LineIterator lineIterator​(File file)
      Returns an Iterator for the lines in a File using the default encoding for the VM
      static org.apache.commons.io.LineIterator lineIterator​(File file, String encoding)
      Returns an Iterator for the lines in a File
      static Collection<File> listFiles​(File directory, org.apache.commons.io.filefilter.IOFileFilter fileFilter, org.apache.commons.io.filefilter.IOFileFilter dirFilter)
      Finds files within a given directory (and optionally its subdirectories).
      static Collection<File> listFilesAndDirs​(File directory, org.apache.commons.io.filefilter.IOFileFilter fileFilter, org.apache.commons.io.filefilter.IOFileFilter dirFilter)
      Finds files within a given directory (and optionally its subdirectories) All files found are filtered by an IOFileFilter.
      static void moveDirectory​(File srcDir, File destDir)
      Moves a directory
      static void moveDirectoryToDirectory​(File src, File destDir, boolean createDestDir)
      Moves a directory to another directory
      static void moveFile​(File srcFile, File destFile)
      Moves a file
      static void moveFileToDirectory​(File srcFile, File destDir, boolean createDestDir)
      Moves a file to a directory
      static void moveToDirectory​(File src, File destDir, boolean createDestDir)
      Moves a file or directory to the destination directory
      static FileInputStream openInputStream​(File file)
      Opens a FileInputStream for the specified file,
      static FileOutputStream openOutputStream​(File file)
      Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.
      static FileOutputStream openOutputStream​(File file, boolean append)
      Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.
      static byte[] readFileToByteArray​(File file)
      Reads the contents of a file into a byte array
      static String readFileToString​(File file, Charset encoding)
      Reads the contents of a file into a String.
      static List<String> readLines​(File file, String encoding)
      Reads the contents of a file line by line to a List of Strings The file is always closed.
      static List<String> readLines​(File file, Charset encoding)
      Reads the contents of a file line by line to a List of Strings The file is always closed
      static long sizeOf​(File file)
      Returns the size of the specified file or directory The return value may be negative if overflow occurs
      static long sizeOfDirectory​(File directory)
      Counts the size of a directory recursively (sum of the length of all files)
      static File toFile​(URL url)
      Convert from a URL to a File
      static File[] toFiles​(URL[] urls)
      Converts each of an array of URL to a File.
      static URL[] toURLs​(File[] files)
      Converts each of an array of File to a URL.
      static boolean waitFor​(File file, int seconds)
      Waits for NFS to propagate a file creation, imposing a timeout.
      static void write​(File file, CharSequence data, String encoding)
      Writes a CharSequence to a file creating the file if it does not exist.
      static void write​(File file, CharSequence data, String encoding, boolean append)
      Writes a CharSequence to a file creating the file if it does not exist.
      static void write​(File file, CharSequence data, Charset encoding)
      Writes a CharSequence to a file creating the file if it does not exist.
      static void write​(File file, CharSequence data, Charset encoding, boolean append)
      Writes a CharSequence to a file creating the file if it does not exist.
      static void writeByteArrayToFile​(File file, byte[] data)
      Writes a byte array to a file creating the file if it does not exist.
      static void writeByteArrayToFile​(File file, byte[] data, boolean append)
      Writes a byte array to a file creating the file if it does not exist.
      static void writeByteArrayToFile​(File file, byte[] data, int off, int len)
      Writes len bytes from the specified byte array starting at offset off to a file, creating the file if it does not exist.
      static void writeByteArrayToFile​(File file, byte[] data, int off, int len, boolean append)
      Writes len bytes from the specified byte array starting at offset off to a file, creating the file if it does not exist.
      static void writeLines​(File file, String encoding, Collection<?> lines)
      Writes the toString() value of each item in a collection to the specified File line by line.
      static void writeLines​(File file, String encoding, Collection<?> lines, boolean append)
      Writes the toString() value of each item in a collection to the specified File line by line, optionally appending.
      static void writeLines​(File file, String encoding, Collection<?> lines, String lineEnding)
      Writes the toString() value of each item in a collection to the specified File line by line.
      static void writeLines​(File file, String encoding, Collection<?> lines, String lineEnding, boolean append)
      Writes the toString() value of each item in a collection to the specified File line by line.
      static void writeLines​(File file, Collection<?> lines)
      Writes the toString() value of each item in a collection to the specified File line by line.
      static void writeLines​(File file, Collection<?> lines, boolean append)
      Writes the toString() value of each item in a collection to the specified File line by line.
      static void writeLines​(File file, Collection<?> lines, String lineEnding)
      Writes the toString() value of each item in a collection to the specified File line by line.
      static void writeLines​(File file, Collection<?> lines, String lineEnding, boolean append)
      Writes the toString() value of each item in a collection to the specified File line by line.
      static void writeStringToFile​(File file, String data, String encoding)
      Writes a String to a file creating the file if it does not exist.
      static void writeStringToFile​(File file, String data, String encoding, boolean append)
      Writes a String to a file creating the file if it does not exist
      static void writeStringToFile​(File file, String data, Charset encoding)
      Writes a String to a file creating the file if it does not exist.
      static void writeStringToFile​(File file, String data, Charset encoding, boolean append)
      Writes a String to a file creating the file if it does not exist
    • Method Detail

      • byteCountToDisplaySize

        public static String byteCountToDisplaySize​(long size)
        Returns a human-readable version of the file size, where the input represents a specific number of bytes.
        Parameters:
        size - Size of the file
        Returns:
        a human-readable version of the file size
      • checksum

        public static Checksum checksum​(File file,
                                        Checksum checksum)
                                 throws IOException
        Computes the checksum of a file using the specified checksum object
        Parameters:
        file - Input file to checksum
        checksum - The checksum object to be used
        Returns:
        value of the checksum
        Throws:
        IllegalArgumentException - if the file is a directory.
        IOException - if an IO error occurs reading the file
        NullPointerException - if the file or checksum is null
      • checksumCRC32

        public static long checksumCRC32​(File file)
                                  throws IOException
        Computes the checksum of a file using the CRC32 checksum routine
        Parameters:
        file - Input file to checksum
        Returns:
        value of the checksum
        Throws:
        IllegalArgumentException - if the file is a directory.
        IOException - if an IO error occurs reading the file.
        NullPointerException - if the file or checksum is null.
      • cleanDirectory

        public static void cleanDirectory​(File directory)
                                   throws IOException
        Cleans a directory without deleting it
        Parameters:
        directory - Input directory to clean
        Throws:
        IOException - in case cleaning is unsuccessful.
        IllegalArgumentException - if directory does not exist or is not a directory.
      • contentEquals

        public static boolean contentEquals​(File file1,
                                            File file2)
                                     throws IOException
        Compares the contents of two files to determine if they are equal or not
        Parameters:
        file1 - the first file
        file2 - the second file
        Returns:
        true if the content of the files are equal or they both don't exist, false otherwise
        Throws:
        IOException - in case of an I/O error.
      • contentEqualsIgnoreEOL

        public static boolean contentEqualsIgnoreEOL​(File file1,
                                                     File file2,
                                                     String charsetName)
                                              throws IOException
        Compares the contents of two files to determine if they are equal or not. This method checks to see if the two files point to the same file, before resorting to line-by-line comparison of the contents.
        Parameters:
        file1 - the first file
        file2 - the second file
        charsetName - the character encoding to be used
        Returns:
        true if the content of the files are equal or neither exists, false otherwise
        Throws:
        IOException - in case of an I/O error.
      • convertFileCollectionToFileArray

        public static File[] convertFileCollectionToFileArray​(Collection<File> files)
        Converts a Collection containing java.io.File instanced into array representation
        Parameters:
        files - a Collection containing java.io.File instances
        Returns:
        an array of java.io.File
      • copyDirectory

        public static void copyDirectory​(File srcDir,
                                         File destDir)
                                  throws IOException
        Copies a whole directory to a new location preserving the file dates
        Parameters:
        srcDir - an existing directory to copy
        destDir - the new directory
        Throws:
        NullPointerException - if source or destination is null.
        IOException - if source or destination is invalid or if an IO error occurs during copying.
      • copyFile

        public static void copyFile​(File srcFile,
                                    File destFile)
                             throws IOException
        Copies a file to a new location preserving the file date
        Parameters:
        srcFile - an existing file to copy
        destFile - the new file
        Throws:
        NullPointerException - if source or destination is null.
        IOException - if source or destination is invalid or if an IO error occurs during copying or if the output file length is not the same as the input file length after the copy completes.
      • copyFile

        public static long copyFile​(File input,
                                    OutputStream output)
                             throws IOException
        Copy bytes from a File to an OutputStream.
        Parameters:
        input - the file to read from
        output - the OutputStream to write to
        Returns:
        the number of bytes copied
        Throws:
        IOException - if an I/O error occurs.
        NullPointerException - if the input or output is null.
      • copyInputStreamToFile

        public static void copyInputStreamToFile​(InputStream source,
                                                 File destination)
                                          throws IOException
        Copies bytes from an InputStream source to a file destination.
        Parameters:
        source - the InputStream to copy bytes from
        destination - the non-directory File to write bytes to (possibly overwriting)
        Throws:
        IOException - if destination is a directory or if destination cannot be written or if destination needs creating but can't be or if an IO error occurs during copying.
      • copyToFile

        public static void copyToFile​(InputStream source,
                                      File destination)
                               throws IOException
        Copies bytes from an InputStream source to a file destination The source stream is left open
        Parameters:
        source - the InputStream to copy bytes from
        destination - the non-directory File to write bytes to (possibly overwriting)
        Throws:
        IOException - if destination is a directory or if destination cannot be written or if destination needs creating but can't be or if an IO error occurs during copying.
      • deleteDirectory

        public static void deleteDirectory​(File directory)
                                    throws IOException
        Deletes a directory recursively
        Parameters:
        directory - directory to delete
        Throws:
        IOException - in case deletion is unsuccessful.
        IllegalArgumentException - if directory does not exist or is not a directory.
      • deleteQuietly

        public static boolean deleteQuietly​(File file)
        Deletes a file, never throwing an exception
        Parameters:
        file - file or directory to delete
        Returns:
        true if the file or directory was deleted, otherwise false
      • directoryContains

        public static boolean directoryContains​(File directory,
                                                File child)
                                         throws IOException
        Determines whether the parent directory contains the child element (a file or directory)
        Parameters:
        directory - the parent directory
        child - the child file or directory
        Returns:
        true is the candidate leaf is under by the specified composite. False otherwise
        Throws:
        IOException - if an IO error occurs while checking the files.
        IllegalArgumentException - if directory is null or not a directory.
      • forceDeleteOnExit

        public static void forceDeleteOnExit​(File file)
                                      throws IOException
        Schedules a file to be deleted when JVM exits
        Parameters:
        file - file or directory to delete
        Throws:
        IOException - in case deletion is unsuccessful.
        NullPointerException - if the file is null.
      • getFile

        public static File getFile​(File directory,
                                   String... names)
        Construct a file from the set of name elements.
        Parameters:
        directory - the parent directory
        names - the name elements
        Returns:
        file
      • getFile

        public static File getFile​(String... names)
        Construct a file from the set of name elements.
        Parameters:
        names - the name elements
        Returns:
        file
      • isFileNewer

        public static boolean isFileNewer​(File file,
                                          Date date)
        Tests if the specified File is newer than the specified Date
        Parameters:
        file - the File of which the modification date must be compared
        date - the date reference
        Returns:
        true if the File exists and has been modified after the given Date
        Throws:
        IllegalArgumentException - if the file is null or if the date is null.
      • isFileOlder

        public static boolean isFileOlder​(File file,
                                          Date date)
        Tests if the specified File is older than the specified Date
        Parameters:
        file - the File of which the modification date must be compared
        date - the date reference
        Returns:
        true if the File exists and has been modified before the given Date
        Throws:
        IllegalArgumentException - if the file is null or if the date is null.
      • isSymlink

        public static boolean isSymlink​(File file)
                                 throws IOException
        Determines whether the specified file is a Symbolic Link rather than an actual file
        Parameters:
        file - the file to check
        Returns:
        true if the file is a Symbolic Link
        Throws:
        IOException - if an IO error occurs while checking the file.
      • iterateFiles

        public static Iterator<File> iterateFiles​(File directory,
                                                  org.apache.commons.io.filefilter.IOFileFilter fileFilter,
                                                  org.apache.commons.io.filefilter.IOFileFilter dirFilter)
        Allows iteration over the files in given directory (and optionally its sub directories). All files found are filtered by an IOFileFilter
        Parameters:
        directory - the directory to search in
        fileFilter - filter to apply when finding files
        dirFilter - optional filter to apply when finding subdirectories
        Returns:
        an iterator of java.io.File for the matching files
      • lineIterator

        public static org.apache.commons.io.LineIterator lineIterator​(File file)
                                                               throws IOException
        Returns an Iterator for the lines in a File using the default encoding for the VM
        Parameters:
        file - the file to open for input
        Returns:
        an Iterator of the lines in the file, never null
        Throws:
        IOException - in case of an I/O error (file closed).
      • lineIterator

        public static org.apache.commons.io.LineIterator lineIterator​(File file,
                                                                      String encoding)
                                                               throws IOException
        Returns an Iterator for the lines in a File
        Parameters:
        file - the file to open for input
        encoding - the encoding to use
        Returns:
        an Iterator of the lines in the file, never null
        Throws:
        IOException - in case of an I/O error (file closed).
      • listFiles

        public static Collection<File> listFiles​(File directory,
                                                 org.apache.commons.io.filefilter.IOFileFilter fileFilter,
                                                 org.apache.commons.io.filefilter.IOFileFilter dirFilter)
        Finds files within a given directory (and optionally its subdirectories). All files found are filtered by an IOFileFilter.
        Parameters:
        directory - the directory to search in
        fileFilter - filter to apply when finding files
        dirFilter - optional filter to apply when finding subdirectories
        Returns:
        an collection of java.io.File with the matching files
      • listFilesAndDirs

        public static Collection<File> listFilesAndDirs​(File directory,
                                                        org.apache.commons.io.filefilter.IOFileFilter fileFilter,
                                                        org.apache.commons.io.filefilter.IOFileFilter dirFilter)
        Finds files within a given directory (and optionally its subdirectories) All files found are filtered by an IOFileFilter. The resulting collection includes the starting directory and any subdirectories that match the directory filter
        Parameters:
        directory - the directory to search in
        fileFilter - filter to apply when finding files
        dirFilter - optional filter to apply when finding subdirectories
        Returns:
        an collection of java.io.File with the matching files
      • moveDirectory

        public static void moveDirectory​(File srcDir,
                                         File destDir)
                                  throws IOException
        Moves a directory
        Parameters:
        srcDir - the directory to be moved
        destDir - the destination directory
        Throws:
        FileExistsException - if the destination directory exists.
        IOException - if an IO error occurs moving the file or if source or destination is invalid.
        NullPointerException - if source or destination is null.
      • moveDirectoryToDirectory

        public static void moveDirectoryToDirectory​(File src,
                                                    File destDir,
                                                    boolean createDestDir)
                                             throws IOException
        Moves a directory to another directory
        Parameters:
        src - the file to be moved
        destDir - the destination file
        createDestDir - If true create the destination directory, otherwise if false throw an java.io.IOException
        Throws:
        FileExistsException - if the directory exists in the destination directory.
        IOException - if source or destination is invalid or if an IO error occurs moving the file.
        NullPointerException - if source or destination is null.
      • moveFile

        public static void moveFile​(File srcFile,
                                    File destFile)
                             throws IOException
        Moves a file
        Parameters:
        srcFile - the file to be moved
        destFile - the destination file
        Throws:
        FileExistsException - if the destination file exists.
        IOException - if source or destination is invalid or if an IO error occurs moving the file.
        NullPointerException - if source or destination is null.
      • moveFileToDirectory

        public static void moveFileToDirectory​(File srcFile,
                                               File destDir,
                                               boolean createDestDir)
                                        throws IOException
        Moves a file to a directory
        Parameters:
        srcFile - the file to be moved
        destDir - he destination file
        createDestDir - If true create the destination directory, otherwise if false throw an java.io.IOException
        Throws:
        FileExistsException - if the destination file exists.
        IOException - if source or destination is invalid or if an IO error occurs moving the file.
        NullPointerException - if source or destination is null.
      • moveToDirectory

        public static void moveToDirectory​(File src,
                                           File destDir,
                                           boolean createDestDir)
                                    throws IOException
        Moves a file or directory to the destination directory
        Parameters:
        src - the file or directory to be moved
        destDir - the destination directory
        createDestDir - If true create the destination directory, otherwise if false throw an java.io.IOException
        Throws:
        FileExistsException - if the directory or file exists in the destination directory.
        IOException - if an IO error occurs moving the file or if source or destination is invalid.
        NullPointerException - if source or destination is null.
      • openInputStream

        public static FileInputStream openInputStream​(File file)
                                               throws IOException
        Opens a FileInputStream for the specified file,
        Parameters:
        file - the file to open for input
        Returns:
        a new FileInputStream for the specified file
        Throws:
        FileNotFoundException - if the file does not exist.
        IOException - if the file cannot be read.
      • openOutputStream

        public static FileOutputStream openOutputStream​(File file)
                                                 throws IOException
        Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.
        Parameters:
        file - the file to open for output
        Returns:
        a new FileOutputStream for the specified file
        Throws:
        IOException - if the file object is a directory or if a parent directory needs creating but that fails or if the file cannot be written to.
      • openOutputStream

        public static FileOutputStream openOutputStream​(File file,
                                                        boolean append)
                                                 throws IOException
        Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.
        Parameters:
        file - the file to open for output
        append - if true, then bytes will be added to the end of the file rather than overwriting
        Returns:
        a new FileOutputStream for the specified file
        Throws:
        IOException - if the file object is a directory or if a parent directory needs creating but that fails or if the file cannot be written to.
      • readFileToByteArray

        public static byte[] readFileToByteArray​(File file)
                                          throws IOException
        Reads the contents of a file into a byte array
        Parameters:
        file - the file to read
        Returns:
        the file contents
        Throws:
        IOException - in case of an I/O error.
      • readFileToString

        public static String readFileToString​(File file,
                                              Charset encoding)
                                       throws IOException
        Reads the contents of a file into a String. The file is always closed.
        Parameters:
        file - the file to read
        encoding - the encoding to use
        Returns:
        the file contents
        Throws:
        IOException - in case of an I/O error.
      • readLines

        public static List<String> readLines​(File file,
                                             Charset encoding)
                                      throws IOException
        Reads the contents of a file line by line to a List of Strings The file is always closed
        Parameters:
        file - the file to read
        encoding - the encoding to use
        Returns:
        the list of Strings representing each line in the file, never null
        Throws:
        IOException - in case of an I/O error.
      • readLines

        public static List<String> readLines​(File file,
                                             String encoding)
                                      throws IOException
        Reads the contents of a file line by line to a List of Strings The file is always closed.
        Parameters:
        file - the file to read
        encoding - the encoding to use
        Returns:
        the list of Strings representing each line in the file, never null
        Throws:
        IOException - in case of an I/O error.
        UnsupportedCharsetException - thrown instead of .UnsupportedEncodingException in version 2.2 if the encoding is not supported.
      • sizeOf

        public static long sizeOf​(File file)
        Returns the size of the specified file or directory The return value may be negative if overflow occurs
        Parameters:
        file - the file or directory to return the size of
        Returns:
        the length of the file, or recursive size of the directory, provided (in bytes)
        Throws:
        NullPointerException - if the file is null.
        IllegalArgumentException - if the file does not exist.
      • sizeOfDirectory

        public static long sizeOfDirectory​(File directory)
        Counts the size of a directory recursively (sum of the length of all files)
        Parameters:
        directory - directory to inspect
        Returns:
        size of directory in bytes
        Throws:
        NullPointerException - if the directory is null.
      • toFile

        public static File toFile​(URL url)
        Convert from a URL to a File
        Parameters:
        url - the file URL to convert
        Returns:
        the equivalent File object, or null if the URL's protocol is not file
      • toFiles

        public static File[] toFiles​(URL[] urls)
        Converts each of an array of URL to a File.
        Parameters:
        urls - the file URLs to convert,
        Returns:
        a non-null array of Files matching the input, with a null item if there was a null at that index in the input array
        Throws:
        IllegalArgumentException - if any file is not a URL file or if any file is incorrectly encoded.
      • toURLs

        public static URL[] toURLs​(File[] files)
                            throws IOException
        Converts each of an array of File to a URL.
        Parameters:
        files - the files to convert
        Returns:
        an array of URLs matching the input
        Throws:
        IOException - if a file cannot be converted.
        NullPointerException - if the parameter is null.
      • waitFor

        public static boolean waitFor​(File file,
                                      int seconds)
        Waits for NFS to propagate a file creation, imposing a timeout.
        Parameters:
        file - the file to check
        seconds - the maximum time in seconds to wait
        Returns:
        true if file exists
        Throws:
        NullPointerException - if the file is null.
      • write

        public static void write​(File file,
                                 CharSequence data,
                                 String encoding)
                          throws IOException
        Writes a CharSequence to a file creating the file if it does not exist.
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use
        Throws:
        IOException - in case of an I/O error.
        UnsupportedEncodingException - if the encoding is not supported by the VM.
      • write

        public static void write​(File file,
                                 CharSequence data,
                                 Charset encoding)
                          throws IOException
        Writes a CharSequence to a file creating the file if it does not exist.
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use
        Throws:
        IOException - in case of an I/O error.
      • write

        public static void write​(File file,
                                 CharSequence data,
                                 Charset encoding,
                                 boolean append)
                          throws IOException
        Writes a CharSequence to a file creating the file if it does not exist.
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use
        append - if true, then the data will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
      • write

        public static void write​(File file,
                                 CharSequence data,
                                 String encoding,
                                 boolean append)
                          throws IOException
        Writes a CharSequence to a file creating the file if it does not exist.
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use
        append - if true, then the data will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
        UnsupportedCharsetException - if the encoding is not supported by the VM.
      • writeByteArrayToFile

        public static void writeByteArrayToFile​(File file,
                                                byte[] data)
                                         throws IOException
        Writes a byte array to a file creating the file if it does not exist.
        Parameters:
        file - the file to write to
        data - the content to write to the file
        Throws:
        IOException - in case of an I/O error.
      • writeByteArrayToFile

        public static void writeByteArrayToFile​(File file,
                                                byte[] data,
                                                boolean append)
                                         throws IOException
        Writes a byte array to a file creating the file if it does not exist.
        Parameters:
        file - the file to write to
        data - the content to write to the file
        append - if true, then bytes will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
      • writeByteArrayToFile

        public static void writeByteArrayToFile​(File file,
                                                byte[] data,
                                                int off,
                                                int len)
                                         throws IOException
        Writes len bytes from the specified byte array starting at offset off to a file, creating the file if it does not exist.
        Parameters:
        file - the file to write to
        data - the content to write to the file
        off - the start offset in the data
        len - the number of bytes to write
        Throws:
        IOException - in case of an I/O error.
      • writeByteArrayToFile

        public static void writeByteArrayToFile​(File file,
                                                byte[] data,
                                                int off,
                                                int len,
                                                boolean append)
                                         throws IOException
        Writes len bytes from the specified byte array starting at offset off to a file, creating the file if it does not exist.
        Parameters:
        file - the file to write to
        data - the content to write to the file
        off - the start offset in the data
        len - the number of bytes to write
        append - if true, then bytes will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
      • writeLines

        public static void writeLines​(File file,
                                      Collection<?> lines)
                               throws IOException
        Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.
        Parameters:
        file - the file to write to
        lines - the lines to write
        Throws:
        IOException - in case of an I/O error.
      • writeLines

        public static void writeLines​(File file,
                                      Collection<?> lines,
                                      boolean append)
                               throws IOException
        Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.
        Parameters:
        file - the file to write to
        lines - the lines to write
        append - if true, then the lines will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
      • writeLines

        public static void writeLines​(File file,
                                      String encoding,
                                      Collection<?> lines,
                                      String lineEnding,
                                      boolean append)
                               throws IOException
        Writes the toString() value of each item in a collection to the specified File line by line. The specified character encoding and the line ending will be used.
        Parameters:
        file - the file to write to
        encoding - the encoding to use
        lines - the lines to write
        lineEnding - the line separator to use
        append - if true, then the lines will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
        UnsupportedEncodingException - if the encoding is not supported by the VM.
      • writeLines

        public static void writeLines​(File file,
                                      Collection<?> lines,
                                      String lineEnding)
                               throws IOException
        Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the specified line ending will be used.
        Parameters:
        file - the file to write to
        lines - the lines to write
        lineEnding - the line separator to use
        Throws:
        IOException - in case of an I/O error.
      • writeLines

        public static void writeLines​(File file,
                                      String encoding,
                                      Collection<?> lines,
                                      String lineEnding)
                               throws IOException
        Writes the toString() value of each item in a collection to the specified File line by line. The specified character encoding and the line ending will be used.
        Parameters:
        file - the file to write to
        encoding - the encoding to use
        lines - the lines to write
        lineEnding - the line separator to use
        Throws:
        IOException - in case of an I/O error.
        UnsupportedEncodingException - if the encoding is not supported by the VM.
      • writeLines

        public static void writeLines​(File file,
                                      String encoding,
                                      Collection<?> lines)
                               throws IOException
        Writes the toString() value of each item in a collection to the specified File line by line. The specified character encoding and the default line ending will be used.
        Parameters:
        file - the file to write to
        encoding - the encoding to use
        lines - the lines to write
        Throws:
        IOException - in case of an I/O error.
        UnsupportedEncodingException - if the encoding is not supported by the VM.
      • writeLines

        public static void writeLines​(File file,
                                      String encoding,
                                      Collection<?> lines,
                                      boolean append)
                               throws IOException
        Writes the toString() value of each item in a collection to the specified File line by line, optionally appending. The specified character encoding and the default line ending will be used.
        Parameters:
        file - the file to write to
        encoding - the encoding to use
        lines - the lines to write
        append - if true, then the lines will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
        UnsupportedEncodingException - if the encoding is not supported by the VM.
      • writeLines

        public static void writeLines​(File file,
                                      Collection<?> lines,
                                      String lineEnding,
                                      boolean append)
                               throws IOException
        Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the specified line ending will be used.
        Parameters:
        file - the file to write to
        lines - the lines to write
        lineEnding - the line separator to use
        append - if true, then the lines will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
      • writeStringToFile

        public static void writeStringToFile​(File file,
                                             String data,
                                             Charset encoding)
                                      throws IOException
        Writes a String to a file creating the file if it does not exist.
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use
        Throws:
        IOException - in case of an I/O error.
        UnsupportedEncodingException - if the encoding is not supported by the VM.
      • writeStringToFile

        public static void writeStringToFile​(File file,
                                             String data,
                                             String encoding,
                                             boolean append)
                                      throws IOException
        Writes a String to a file creating the file if it does not exist
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use
        append - if true, then the String will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.
        UnsupportedCharsetException - if the encoding is not supported by the VM.
      • writeStringToFile

        public static void writeStringToFile​(File file,
                                             String data,
                                             String encoding)
                                      throws IOException
        Writes a String to a file creating the file if it does not exist.
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use
        Throws:
        IOException - in case of an I/O error.
        UnsupportedEncodingException - if the encoding is not supported by the VM.
      • writeStringToFile

        public static void writeStringToFile​(File file,
                                             String data,
                                             Charset encoding,
                                             boolean append)
                                      throws IOException
        Writes a String to a file creating the file if it does not exist
        Parameters:
        file - the file to write
        data - the content to write to the file
        encoding - the encoding to use
        append - if true, then the String will be added to the end of the file rather than overwriting
        Throws:
        IOException - in case of an I/O error.