Package com.avoka.core.util
Class FileUtils
- java.lang.Object
-
- com.avoka.core.util.FileUtils
-
public class FileUtils extends Object
Provides File utility methods.
-
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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(Reader reader)
Close a reader.static void
close(Writer writer)
Close a writer.static void
close(ZipFile zipFile)
Close a zipped file.static void
copyFile(File sourceFile, File destinationFile)
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
moveFile(File sourceFile, File destinationFile)
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.static List<String>
readTextFile(File file)
Return the file content and return a list of the lines of text in the file.static List<String>
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[]
zip(byte[] fileData, String fileName)
Given a byte array and a file name, creates a ZIP file containing the input data as a single entry.
-
-
-
Method Detail
-
formatFileSize
public static String formatFileSize(Number byteSize)
Return a String notation of file size in MB or KB.- Parameters:
byteSize
- the size of a file- Returns:
- a String of file size
-
formatFileSizeKB
public static String formatFileSizeKB(Number byteSize)
Return a String notation of file size notation in KB.- Parameters:
byteSize
- the size of a file- Returns:
- a String notation of file size
-
formatFileSizeMB
public static String formatFileSizeMB(Number byteSize)
Return a String notation of file size notation in MB.- Parameters:
byteSize
- the size of a file- Returns:
- a String notation of file size
-
readTextFile
public static List<String> readTextFile(String fileName) throws IOException
Return the file content and return a list of the lines of text in the file.- Parameters:
fileName
- the file name to read- Returns:
- An list of the lines of text in the file
- Throws:
IOException
- will be thrown if it can't read from the file
-
readTextFile
public static List<String> readTextFile(File file) throws IOException
Return the file content and return a list of the lines of text in the file.- Parameters:
file
- a file reference- Returns:
- An list of the lines of text in the file
- Throws:
IOException
- will be thrown if it can't read from the file
-
writeToFile
public static 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
public static void writeToFile(File file, String str) throws FileNotFoundException, IOException
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
public static void writeToFile(File file, byte[] byteArr) throws FileNotFoundException, IOException
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
public static String getFileMD5(File file) throws FileNotFoundException, IOException
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
public static StringBuffer readFileAsString(File file) throws FileNotFoundException, IOException
Read a file into a StringBuffer and return the StringBuffer reference.- Parameters:
file
- the file reference- Returns:
- the string buffer of the file content
- Throws:
FileNotFoundException
- will be thrown if file is not foundIOException
- will be thrown if it can't read from the file
-
readFileAsByteArray
public static byte[] readFileAsByteArray(File file) throws FileNotFoundException, IOException
Read a file into a byte array and return the byte array reference.- Parameters:
file
- the file reference- Returns:
- the byte array reference of the file content
- Throws:
FileNotFoundException
- will be thrown if file is not 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
public static boolean checkDirectoryExists(File fileToCheck)
Check whether the directory exists.- Parameters:
fileToCheck
- file reference to check- Returns:
- true if the directory exists.
-
ensureDirectoryExists
public static boolean ensureDirectoryExists(String path)
Create directory path if the directory does not exist.- Parameters:
path
- directory path- Returns:
- true if the directory was created, along with all necessary parent directories
-
createDateSubdirectory
public static File createDateSubdirectory(File directory) throws IOException
Creates a date subdirectory under a given directory.- Parameters:
directory
- a directory to add the date directory- Returns:
- a file reference to the created date subdirectory
- Throws:
IOException
- will be thrown if it can't read from the file
-
fileNameSplit
public static String[] fileNameSplit(String fileName)
Returns the file name and file extension.- Parameters:
fileName
- the full file name e.g. notice.txt- Returns:
- the file name and file extension
-
getFileExtension
public static String getFileExtension(String fileName)
Return the file extension.- Parameters:
fileName
- the full file name- Returns:
- the file extension
-
changeFileExtension
public static String changeFileExtension(String fileName, String newExtension)
Change a file extension name.- Parameters:
fileName
- full file name to be changednewExtension
- the new extension to change- Returns:
- the file name after change
-
getFileName
public static String getFileName(String fileNameWithExtension)
Return the file name without extension.- Parameters:
fileNameWithExtension
- the full file name- Returns:
- the file name without extension
-
getOldestFile
public static File getOldestFile(File[] fileArray)
Gets the oldest file in the list.- Parameters:
fileArray
- containing a names of files (as returned by File.listFiles()- Returns:
- the oldest file in the list
-
getNewestFile
public static File getNewestFile(File[] fileArray)
Gets the newest file in the list.- Parameters:
fileArray
- containing a names of files (as returned by File.listFiles()- Returns:
- the newest file in the list
-
getSmallestFile
public static File getSmallestFile(File[] fileArray)
Gets the smallest file in the list.- Parameters:
fileArray
- containing a names of files (as returned by File.listFiles()- Returns:
- the smallest File
-
getLargetsFile
public static File getLargetsFile(File[] fileArray)
Gets the largest file in the list.- Parameters:
fileArray
- containing a names of files (as returned by File.listFiles()- Returns:
- the largest File
-
setLastModifiedTime
public static boolean setLastModifiedTime(File file, Date dateTime)
This method changes the modified time attribute for the given File. If the Date value is null then the method returns false and the modified time stays untouched.
- Parameters:
file
- - the file to which the modified time has to be 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
public static boolean setLastModifiedTime(File file)
This method changes the modified time attribute for the given File. The method changes the modified time attribute with the current date-time.
- Parameters:
file
- - the file to which the modified time has to be changed- Returns:
- - true if and only if the operation succeeded; false otherwise.
-
close
public static void close(InputStream stream)
Close an input stream.- Parameters:
stream
- an input stream
-
close
public static void close(ZipFile zipFile)
Close a zipped file.- Parameters:
zipFile
- a zipped file
-
close
public static void close(OutputStream stream)
Close an output stream.- Parameters:
stream
- an output stream
-
close
public static void close(Writer writer)
Close a writer.- Parameters:
writer
- a writer
-
close
public static void close(Reader reader)
Close a reader.- Parameters:
reader
- a reader
-
toByteArray
public static byte[] toByteArray(InputStream input) throws IOException
Convert an input stream to a byte array- Parameters:
input
- the input stream- Returns:
- the converted byte array
- Throws:
IOException
- will be thrown if it can't read from the file
-
zip
public static byte[] zip(byte[] fileData, String fileName) throws IOException
Given a byte array and a file name, creates a ZIP file containing the input data as a single entry.- Parameters:
fileData
- the byte array representing the ZIP entry (required)fileName
- the name to use for the ZIP entry (required)- Returns:
- a byte array representing the zipped file data
- Throws:
IOException
- if an error occurs zipping the data
-
-