Package com.avoka.core.util
Class FileUtils
java.lang.Object
com.avoka.core.util.FileUtils
Provides File utility methods.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
changeFileExtension
(String fileName, String newExtension) Change a file extension name.static boolean
checkDirectoryExists
(File fileToCheck) Check whether the directory exists.static void
close
(InputStream stream) Close an input stream.static void
close
(OutputStream stream) Close an output stream.static void
Close a reader.static void
Close a writer.static void
Close a zipped file.static void
Copies one file to another.static void
copyFileOld
(File sourceFile, File destinationFile) See copyFile for a faster version of this.static File
createDateSubdirectory
(File directory) Creates a date subdirectory under a given directory.static boolean
ensureDirectoryExists
(String path) Create directory path if the directory does not exist.static String[]
fileNameSplit
(String fileName) Returns the file name and file extension.static String
formatFileSize
(Number byteSize) Return a String notation of file size in MB or KB.static String
formatFileSizeKB
(Number byteSize) Return a String notation of file size notation in KB.static String
formatFileSizeMB
(Number byteSize) Return a String notation of file size notation in MB.static String
getFileExtension
(String fileName) Return the file extension.static String
getFileMD5
(File file) Return an 32 char MD5 encoded string of a file.static String
getFileName
(String fileNameWithExtension) Return the file name without extension.static File
getLargetsFile
(File[] fileArray) Gets the largest file in the list.static File
getNewestFile
(File[] fileArray) Gets the newest file in the list.static File
getOldestFile
(File[] fileArray) Gets the oldest file in the list.static File
getSmallestFile
(File[] fileArray) Gets the smallest file in the list.static boolean
This tries a file rename first then if that does not work it does a copy.static boolean
moveFileUsingCopy
(File sourceFile, File destinationFile) This tries copy file first and then try to delete the source file.static byte[]
readFileAsByteArray
(File file) Read a file into a byte array and return the byte array reference.static StringBuffer
readFileAsString
(File file) Read a file into a StringBuffer and return the StringBuffer reference.static StringBuffer
readFileAsString
(String filepath) Read a file into a StringBuffer and return the StringBuffer reference.readTextFile
(File file) Return the file content and return a list of the lines of text in the file.readTextFile
(String fileName) Return the file content and return a list of the lines of text in the file.static boolean
setLastModifiedTime
(File file) This method changes the modified time attribute for the given File.static boolean
setLastModifiedTime
(File file, Date dateTime) This method changes the modified time attribute for the given File.static byte[]
toByteArray
(InputStream input) Convert an input stream to a byte arraystatic void
writeToFile
(File file, byte[] byteArr) Write the byte array to the file.static void
writeToFile
(File file, String str) Write the string to the file.static File
writeToFile
(String filepath, byte[] byteArr) Write the byte array to the file.static void
writeToFile
(String filepath, String str) Write the string to the file.static byte[]
Given a byte array and a file name, creates a ZIP file containing the input data as a single entry.
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
formatFileSize
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
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
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
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
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 void writeToFile(String filepath, String str) throws FileNotFoundException, IOException Write the string to the file.- Parameters:
filepath
- the file path to writestr
- the string to write into the file- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- will be thrown if it can't read from the file
-
writeToFile
Write the string to the file.- Parameters:
file
- the file referencestr
- the string to write into the file- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- 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 writebyteArr
- the byte array to write into the file- Returns:
- the file reference
- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- will be thrown if it can't read from the file
-
writeToFile
Write the byte array to the file.- Parameters:
file
- the file referencebyteArr
- the byte array to write into the file- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- will be thrown if it can't read from the file
-
getFileMD5
Return an 32 char MD5 encoded string of a file.- Parameters:
file
- the file reference- Returns:
- an 32 char MD5 encoded string of a file
- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- 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 foundIOException
- will be thrown if it can't read from the file
-
readFileAsString
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 foundIOException
- will be thrown if it can't read from the file
-
readFileAsByteArray
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 foundIOException
- 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 movedestinationFile
- 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 foundIOException
- 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 movedestinationFile
- the destination file reference for the file to move to- Returns:
- true
- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- 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 movedestinationFile
- the destination file reference for the file to move to- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- 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 movedestinationFile
- the destination file reference for the file to move to- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- will be thrown if it can't read from the file
-
checkDirectoryExists
Check whether the directory exists.- Parameters:
fileToCheck
- file reference to check- Returns:
- true if the directory exists.
-
ensureDirectoryExists
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
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
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
Return the file extension.- Parameters:
fileName
- the full file name- Returns:
- the file extension
-
changeFileExtension
Change a file extension name.- Parameters:
fileName
- full file name to be changednewExtension
- the new extension to change- Returns:
- the file name after change
-
getFileName
Return the file name without extension.- Parameters:
fileNameWithExtension
- the full file name- Returns:
- the file name without extension
-
getOldestFile
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
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
Gets the smallest file in the list.- Parameters:
fileArray
- containing a names of files (as returned by File.listFiles()- Returns:
- the smallest File
-
getLargetsFile
Gets the largest file in the list.- Parameters:
fileArray
- containing a names of files (as returned by File.listFiles()- Returns:
- the largest File
-
setLastModifiedTime
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 changeddateTime
- - 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
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
Close an input stream.- Parameters:
stream
- an input stream
-
close
Close a zipped file.- Parameters:
zipFile
- a zipped file
-
close
Close an output stream.- Parameters:
stream
- an output stream
-
close
Close a writer.- Parameters:
writer
- a writer
-
close
Close a reader.- Parameters:
reader
- a reader
-
toByteArray
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
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
-