Package io.mosip.kernel.core.util
Class FileUtils
- java.lang.Object
-
- io.mosip.kernel.core.util.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 StringbyteCountToDisplaySize(long size)Returns a human-readable version of the file size, where the input represents a specific number of bytes.static Checksumchecksum(File file, Checksum checksum)Computes the checksum of a file using the specified checksum objectstatic longchecksumCRC32(File file)Computes the checksum of a file using the CRC32 checksum routinestatic voidcleanDirectory(File directory)Cleans a directory without deleting itstatic booleancontentEquals(File file1, File file2)Compares the contents of two files to determine if they are equal or notstatic booleancontentEqualsIgnoreEOL(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 representationstatic voidcopyDirectory(File srcDir, File destDir)Copies a whole directory to a new location preserving the file datesstatic voidcopyFile(File srcFile, File destFile)Copies a file to a new location preserving the file datestatic longcopyFile(File input, OutputStream output)Copy bytes from a File to an OutputStream.static voidcopyInputStreamToFile(InputStream source, File destination)Copies bytes from an InputStream source to a file destination.static voidcopyToFile(InputStream source, File destination)Copies bytes from an InputStream source to a file destination The source stream is left openstatic voiddeleteDirectory(File directory)Deletes a directory recursivelystatic booleandeleteQuietly(File file)Deletes a file, never throwing an exceptionstatic booleandirectoryContains(File directory, File child)Determines whether the parent directory contains the child element (a file or directory)static voidforceDelete(File file)Deletes a file.static voidforceDeleteOnExit(File file)Schedules a file to be deleted when JVM exitsstatic FilegetFile(File directory, String... names)Construct a file from the set of name elements.static FilegetFile(String... names)Construct a file from the set of name elements.static booleanisFileNewer(File file, Date date)Tests if the specified File is newer than the specified Datestatic booleanisFileOlder(File file, Date date)Tests if the specified File is older than the specified Datestatic booleanisSymlink(File file)Determines whether the specified file is a Symbolic Link rather than an actual filestatic 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.LineIteratorlineIterator(File file)Returns an Iterator for the lines in a File using the default encoding for the VMstatic org.apache.commons.io.LineIteratorlineIterator(File file, String encoding)Returns an Iterator for the lines in a Filestatic 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 voidmoveDirectory(File srcDir, File destDir)Moves a directorystatic voidmoveDirectoryToDirectory(File src, File destDir, boolean createDestDir)Moves a directory to another directorystatic voidmoveFile(File srcFile, File destFile)Moves a filestatic voidmoveFileToDirectory(File srcFile, File destDir, boolean createDestDir)Moves a file to a directorystatic voidmoveToDirectory(File src, File destDir, boolean createDestDir)Moves a file or directory to the destination directorystatic FileInputStreamopenInputStream(File file)Opens a FileInputStream for the specified file,static FileOutputStreamopenOutputStream(File file)Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.static FileOutputStreamopenOutputStream(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 arraystatic StringreadFileToString(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 closedstatic longsizeOf(File file)Returns the size of the specified file or directory The return value may be negative if overflow occursstatic longsizeOfDirectory(File directory)Counts the size of a directory recursively (sum of the length of all files)static FiletoFile(URL url)Convert from a URL to a Filestatic 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 booleanwaitFor(File file, int seconds)Waits for NFS to propagate a file creation, imposing a timeout.static voidwrite(File file, CharSequence data, String encoding)Writes a CharSequence to a file creating the file if it does not exist.static voidwrite(File file, CharSequence data, String encoding, boolean append)Writes a CharSequence to a file creating the file if it does not exist.static voidwrite(File file, CharSequence data, Charset encoding)Writes a CharSequence to a file creating the file if it does not exist.static voidwrite(File file, CharSequence data, Charset encoding, boolean append)Writes a CharSequence to a file creating the file if it does not exist.static voidwriteByteArrayToFile(File file, byte[] data)Writes a byte array to a file creating the file if it does not exist.static voidwriteByteArrayToFile(File file, byte[] data, boolean append)Writes a byte array to a file creating the file if it does not exist.static voidwriteByteArrayToFile(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 voidwriteByteArrayToFile(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 voidwriteLines(File file, String encoding, Collection<?> lines)Writes the toString() value of each item in a collection to the specified File line by line.static voidwriteLines(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 voidwriteLines(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 voidwriteLines(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 voidwriteLines(File file, Collection<?> lines)Writes the toString() value of each item in a collection to the specified File line by line.static voidwriteLines(File file, Collection<?> lines, boolean append)Writes the toString() value of each item in a collection to the specified File line by line.static voidwriteLines(File file, Collection<?> lines, String lineEnding)Writes the toString() value of each item in a collection to the specified File line by line.static voidwriteLines(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 voidwriteStringToFile(File file, String data, String encoding)Writes a String to a file creating the file if it does not exist.static voidwriteStringToFile(File file, String data, String encoding, boolean append)Writes a String to a file creating the file if it does not existstatic voidwriteStringToFile(File file, String data, Charset encoding)Writes a String to a file creating the file if it does not exist.static voidwriteStringToFile(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 checksumchecksum- 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 fileNullPointerException- 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 filefile2- 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 filefile2- the second filecharsetName- 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 copydestDir- 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 copydestFile- 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 fromoutput- 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 fromdestination- 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 fromdestination- 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 directorychild- 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.
-
forceDelete
public static void forceDelete(File file) throws IOException
Deletes a file.- Parameters:
file- file or directory to delete- Throws:
IOException- in case deletion is unsuccessful.FileNotFoundException- if the file was not foundNullPointerException- if the directory is null.
-
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 directorynames- 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 compareddate- 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 compareddate- 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 infileFilter- filter to apply when finding filesdirFilter- 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 inputencoding- 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 infileFilter- filter to apply when finding filesdirFilter- 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 infileFilter- filter to apply when finding filesdirFilter- 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 moveddestDir- 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 moveddestDir- the destination filecreateDestDir- 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 moveddestFile- 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 moveddestDir- he destination filecreateDestDir- 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 moveddestDir- the destination directorycreateDestDir- 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 outputappend- 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 readencoding- 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 readencoding- 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 readencoding- 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 checkseconds- 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 writedata- the content to write to the fileencoding- 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 writedata- the content to write to the fileencoding- 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 writedata- the content to write to the fileencoding- the encoding to useappend- 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 writedata- the content to write to the fileencoding- the encoding to useappend- 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 todata- 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 todata- the content to write to the fileappend- 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 todata- the content to write to the fileoff- the start offset in the datalen- 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 todata- the content to write to the fileoff- the start offset in the datalen- the number of bytes to writeappend- 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 tolines- 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 tolines- the lines to writeappend- 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 toencoding- the encoding to uselines- the lines to writelineEnding- the line separator to useappend- 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 tolines- the lines to writelineEnding- 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 toencoding- the encoding to uselines- the lines to writelineEnding- 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 toencoding- the encoding to uselines- 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 toencoding- the encoding to uselines- the lines to writeappend- 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 tolines- the lines to writelineEnding- the line separator to useappend- 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 writedata- the content to write to the fileencoding- 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 writedata- the content to write to the fileencoding- the encoding to useappend- 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 writedata- the content to write to the fileencoding- 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 writedata- the content to write to the fileencoding- the encoding to useappend- 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.
-
-