public final class FileUtils
extends Object
Modifier and Type | Field and Description |
---|---|
static int |
BUFFER
The buffer.
|
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
addFilesToZip(String folder,
String zipFileName,
List<String> files,
String... password)
Adds files to the zip.
|
static void |
addFilesToZip(String folder,
String zipFileName,
String fileName,
String... password)
Adds files to the zip.
|
static String |
addLastSeparator(String path)
Adds the last separator.
|
static String |
changeExt(String fileName,
String ext)
Change file extension.
|
static String |
changeFileName(String fileName,
String name)
Change file name only.
|
static void |
copyFile(File in,
File out)
Copies file.
|
static void |
copyFile(InputStream in,
File out)
Copies input stream into the file.
|
static void |
copyFilesFromFolderToFolder(String source,
String dest,
String fName)
Copies files from the source folder to the destination.
|
static void |
createZipWithPassword(String zipFileName,
ArrayList<File> filesToAdd,
String passwd,
String encyptionMethod)
Creates the zip with password.
|
static boolean |
deleteFile(String path)
Deletes file or folder.
|
static boolean |
deleteFile(String folder,
String filename)
Deletes file in the folder.
|
static void |
deleteFilesInFolder(String folder,
String fName)
Delete files in the folder.
|
static void |
downloadFile(String downloadUrl,
String fileName)
Download file.
|
static String |
executeReplaceMacros(String sourceFileName,
String fileName)
Execute replace macros.
|
static boolean |
fileExists(String filename)
Checks if file or folder exists.
|
static boolean |
fileExistsAndNotDir(String filename)
File exists and not dir.
|
static String |
getActualFolderName(String folder)
Gets the actual folder name.
|
static byte[] |
getBytesFromFile(File file)
Reads the binary file and returns array of bytes.
|
static String |
getCorrectFolder(String folder)
Cleanup the folder.
|
static String |
getFilename(String name,
String folder,
String ext,
boolean forceExt)
Creates the full name of the file.
|
static String |
getFileName(String original,
String prefix,
String suffix,
String ext,
String extPrefix,
String extSuffix)
Gets the file name.
|
static String |
getFileNameByWildcard(String sourceFileName,
String pattern,
String fileName,
Set<String> alreadyCreated)
Gets the file name by wildcard.
|
static String |
getFileNameUsingAlgoForSuffix(String fileName,
String algo)
Gets the file name with suffix using given suffix algorithm.
|
static String |
getFileNameWithExtFromOtherFilename(String filename,
String filenameWithExt)
Gets the file name with extension from other filename.
|
static String |
getFilenameWithPath(String name,
String folder,
String ext,
boolean forceExt)
Creates the full name of the file.
|
static String |
getFileNameWithSuffix(String fileName,
String suffix)
Gets the file name with suffix.
|
static List<File> |
getFilesByPattern(String pattern,
Comparator<File> comparator)
Gets the files by pattern and order the list using given comparator.
|
static List<File> |
getFilesByPatterns(String... patterns)
Gets the files by patterns.
|
static File[] |
getFilesInFolder(String folder,
String fName)
Gets the files in the folder.
|
static String |
getFirstFolderName(String folder)
Gets the first folder name.
|
static String |
getFolderName(String folder,
String separator)
Gets the folder name by replacing original separators on the separator
argument.
|
static String |
getFullFileName(String folder,
String name,
String ext,
boolean convert)
Gets the full file name.
|
static String |
getFullName(String url,
String path,
String file)
Gets the full name from url, path and file.
|
static String |
getNameWithoutPrefix(String name,
String prefix)
Gets the name without prefix.
|
static String |
getNormalizedFilename(String folder,
String fileName)
Gets the normalized filename.
|
static String |
getNormalizedFolderName(String folder)
Gets the normalized folder name.
|
static String |
getParentFolderName(String folder)
Gets the parent folder name.
|
static TypedKeyValue<String,String> |
getRootFolderAndTheRest(String folder)
Gets the root folder and the rest.
|
static String |
getTempFileName(String name)
Get the temporary file name using given base name (which could be null).
|
static String |
getTempFileNameInDataFolder(String name)
Gets the temp file name in the data folder.
|
static String |
getUnixFolderName(String folder)
Gets the folder name using Unix style separators.
|
static String |
getValidFileName(String fileName,
boolean withExt)
Returns the valid file name.
|
static void |
gzipFile(String sourceFileName,
String destFileName)
Gzip file.
|
static Boolean |
hasWildCard(String fileName)
Checks for wildcards or { or } in the file name.
|
static void |
hideFileOnWindows(String fileName)
Hides file on windows.
|
static boolean |
isFileNameValid(String file)
Checks if filename is valid.
|
static boolean |
isWildCard(String fileName)
Checks for wildcards in the file name.
|
static String |
loadTextFile(String fileName)
Loads text file into the string.
|
static String |
loadTextFile(String fileName,
long maxSize)
Loads text file into the string.
|
static String |
loadTextFileFromResource(String resource)
Loads text file into the string.
|
static String |
merge(String fileToCreate,
File... allFiles)
Merge files
allFiles into the the single
fileToCreate . |
static String |
merge(String fileToCreate,
String header,
String footer,
String... patterns)
Merge text files using given file pattern.
|
static boolean |
mkDir(String name)
Creates a folder.
|
static Object |
readObject(String fileName)
Reads java object from the file.
|
static Object |
readObject(String fileName,
boolean decrypt)
Reads java object from the file.
|
static Object |
readObjectFromJson(String fileName)
Reads java object from the json file.
|
static Object |
readObjectFromJson(String fileName,
boolean decrypt)
Reads java object from the json file.
|
static Object |
readObjectFromJson(String fileName,
Key key)
Reads encrypted java object from the json file.
|
static TypedKeyValue<String,Long> |
readTextFile(String fileName,
long maxSize)
Reads text file into the string.
|
static TypedKeyValue<String,Long> |
readTextFromStream(InputStream inputStream,
long maxSize)
Reads input stream into the string.
|
static String |
removeLastSeparator(String path)
Removes the last separator.
|
static String |
removeWildCard(String name)
Removes the wild card from file name.
|
static File |
saveFile(ByteArrayOutputStream stream,
String name)
Create file using given output stream and file name.
|
static File |
saveTempFile(ByteArrayOutputStream stream,
String name)
Create file in the temporary folder using given stream and file name.
|
static void |
saveTextFile(String fileName,
String text)
Saves the text file.
|
static String |
ungzipFile(String sourceFileName,
String destFolder)
UnGzip file.
|
static void |
unzip(String zipFilePath,
String destDirectory)
Extracts a zip file specified by the zipFilePath to a directory specified
by destDirectory (will be created if does not exists).
|
static boolean |
unzip(String destFolder,
String wildCard,
boolean deleteZip,
String... password)
Unzip files.
|
static List<String> |
unzipFiles(String destFolder,
String zipFileName,
String... password)
Unzip files into the folder.
|
static List<String> |
unZipFileWithCommons(String destFolder,
String zipFileName,
boolean isFlat,
List<String> files)
Unzip file using commons-compress libary.
|
static List<String> |
unzipOrUnGzipFiles(boolean isUnGZip,
boolean isFlat,
String destFolder,
String zipFileName,
String... password)
Unzip or ungzip files into the folder.
|
static List<String> |
unzipOrUnGzipFiles(boolean isUnGZip,
String destFolder,
String zipFileName,
String... password)
Unzip or ungzip files into the folder.
|
static TypedKeyValue<String,String> |
updateFolderAndFilename(String folder,
String fileName)
Update folder and filename with path by removing path from the file name
and adding it to the folder
Examples: updateFolderAndFilename("c:\temp", "test.txt") --> c:\temp\ test.txt updateFolderAndFilename("c:\temp", "\abc\test.txt") --> c:\temp\abc test.txt updateFolderAndFilename(null, "\abc\test.txt") --> abc test.txt |
static void |
writeObject(String fileName,
Object object)
Writes Java object into the file.
|
static void |
writeObject(String fileName,
Object object,
boolean encrypt)
Writes Java object into the file.
|
static void |
writeObjectToJson(String fileName,
Object object)
Writes Java object into the file in json format.
|
static void |
writeObjectToJson(String fileName,
Object object,
boolean encrypt)
Writes Java object into the file in json format.
|
static void |
writeObjectToJson(String fileName,
Object object,
Key key)
Writes Java object into the file in json format and encrypt the file.
|
static boolean |
zip(String zipFileName,
boolean deleteFiles,
String password,
String encyptionMethod,
String... wildCards)
Zip files.
|
public static void addFilesToZip(String folder, String zipFileName, List<String> files, String... password) throws Exception
folder
- the folder with the fileszipFileName
- the zip file namefiles
- the list of the files to add. The file path is ignored, only
name and extension combined with the folder argument are usedpassword
- the passwordException
- is anything goes wrongpublic static void addFilesToZip(String folder, String zipFileName, String fileName, String... password) throws Exception
folder
- the folder with the fileszipFileName
- the zip file namefileName
- the file name to add. Wildcards are allowed. The file path is
ignored, only name and extension combined with the folder
argument are usedpassword
- the passwordException
- is anything goes wrongpublic static String changeExt(String fileName, String ext)
c:\abc\xyz.txt, new ext xml -- c:\abc\xyz.xml xyz.txt, new ext xml -- xyz.xml xyz, new ext xml -- xyz.xml
fileName
- the file nameext
- the new extensionpublic static String changeFileName(String fileName, String name)
c:\abc\xyz.txt, new name mnm -- c:\abc\mnm.txt xyz, new name mnm -- mnm xyz.txt, new name mnm -- mnm.txt
fileName
- the file namename
- the new namepublic static void copyFile(File in, File out) throws Exception
in
- the input fileout
- the output fileException
- is anything goes wrongpublic static void copyFile(InputStream in, File out) throws Exception
in
- the input streamout
- the output fileException
- in case of any errorpublic static void copyFilesFromFolderToFolder(String source, String dest, String fName) throws Exception
source
- the source folder namedest
- the destination folder namefName
- the file name. Wildcards are allowedException
- in case of any errorpublic static void createZipWithPassword(String zipFileName, ArrayList<File> filesToAdd, String passwd, String encyptionMethod) throws Exception
zipFileName
- the zip file namefilesToAdd
- the files to addpasswd
- the passwordencyptionMethod
- the encyption methodException
- in case of any errorpublic static boolean deleteFile(String path)
path
- the full path to the file or folderpublic static boolean deleteFile(String folder, String filename)
folder
- the folder namefilename
- the file namepublic static void deleteFilesInFolder(String folder, String fName) throws Exception
folder
- the folder namefName
- the file name. Wild cards are allowed.Exception
- in case of any errorpublic static void downloadFile(String downloadUrl, String fileName) throws Exception
downloadUrl
- the download urlfileName
- the file nameException
- the exceptionpublic static boolean fileExists(String filename)
filename
- the file or folder namepublic static boolean fileExistsAndNotDir(String filename)
filename
- the filenamepublic static byte[] getBytesFromFile(File file) throws IOException
file
- the full file nameIOException
- Signals that an I/O exception has occurred.public static String getFilename(String name, String folder, String ext, boolean forceExt)
name
- the original name of the filefolder
- the folderext
- the extensionforceExt
- if true
the ext argument will used. Otherwise the
original file extension is used.public static TypedKeyValue<String,String> updateFolderAndFilename(String folder, String fileName)
folder
- the folderfileName
- the file namepublic static String getFilenameWithPath(String name, String folder, String ext, boolean forceExt)
name
- the original name of the filefolder
- the folderext
- the extensionforceExt
- if true
the ext argument will used. Otherwise the
original file extension is used.public static String getFileName(String original, String prefix, String suffix, String ext, String extPrefix, String extSuffix)
original
- the originalprefix
- the prefixsuffix
- the suffixext
- the extextPrefix
- the ext prefixextSuffix
- the ext suffixpublic static List<File> getFilesByPatterns(String... patterns)
patterns
- the patternspublic static List<File> getFilesByPattern(String pattern, Comparator<File> comparator)
pattern
- the patterncomparator
- the comparatorpublic static File[] getFilesInFolder(String folder, String fName)
folder
- the folder namefName
- the file name. Wildcards are allowedpublic static String getFolderName(String folder, String separator)
folder
- the original folderseparator
- the separatorpublic static String getFullFileName(String folder, String name, String ext, boolean convert)
folder
- the folder namename
- the name of the fileext
- the extension of the fileconvert
- if true
converts all separators to the Unix stylepublic static String getFullName(String url, String path, String file)
url
- the urlpath
- the pathfile
- the filepublic static String getTempFileName(String name)
name
- the namepublic static String getTempFileNameInDataFolder(String name)
name
- the namepublic static String getUnixFolderName(String folder)
folder
- the original folder namepublic static String getValidFileName(String fileName, boolean withExt)
fileName
- the file namewithExt
- if false . will be replaced on _public static boolean isFileNameValid(String file)
file
- the file namepublic static void gzipFile(String sourceFileName, String destFileName) throws IOException
sourceFileName
- the source file namedestFileName
- the gzip file nameIOException
- Signals that an I/O exception has occurred.public static String ungzipFile(String sourceFileName, String destFolder) throws IOException
sourceFileName
- the gzip file namedestFolder
- the destination folderIOException
- Signals that an I/O exception has occurred.public static Boolean hasWildCard(String fileName)
fileName
- the file nameBoolean.TRUE
if file name contains wildcards or
{ or }. Returns null if file name is empty.public static boolean isWildCard(String fileName)
fileName
- the file namepublic static void hideFileOnWindows(String fileName)
fileName
- the file namepublic static String loadTextFile(String fileName) throws IOException
fileName
- the file nameIOException
- Signals that an I/O exception has occurred.public static String loadTextFile(String fileName, long maxSize) throws IOException
fileName
- the file namemaxSize
- the max size of the loaded data. If equals to -1 the limit
will not be enforced.IOException
- Signals that an I/O exception has occurred.public static String loadTextFileFromResource(String resource) throws IOException
resource
- the resourceIOException
- Signals that an I/O exception has occurred.public static String merge(String fileToCreate, File... allFiles) throws Exception
allFiles
into the the single
fileToCreate
.fileToCreate
- the file to createallFiles
- the files to mergeException
- in case of any errrorpublic static String merge(String fileToCreate, String header, String footer, String... patterns) throws Exception
fileToCreate
- the name of the file to createheader
- the header string to be added to the filefooter
- the footer string to be added to the filepatterns
- the array of patterns. For example usr/temp/*.txt, abc.txtException
- the exceptionpublic static boolean mkDir(String name)
name
- the folder namepublic static Object readObject(String fileName) throws Exception
fileName
- the file nameException
- in case of any errorpublic static Object readObject(String fileName, boolean decrypt) throws Exception
fileName
- the file namedecrypt
- if true decrypts the object using default keyException
- in case of any errorpublic static Object readObjectFromJson(String fileName) throws Exception
fileName
- the file nameException
- in case of any errorpublic static Object readObjectFromJson(String fileName, boolean decrypt) throws Exception
fileName
- the file namedecrypt
- if true decrypts the object using default keyException
- in case of any errorpublic static Object readObjectFromJson(String fileName, Key key) throws Exception
fileName
- the file namekey
- the keyException
- in case of any errorpublic static TypedKeyValue<String,Long> readTextFile(String fileName, long maxSize) throws IOException
fileName
- the file namemaxSize
- the max size of the loaded data. If equals to -1 the limit
will not be enforced.IOException
- Signals that an I/O exception has occurred.public static TypedKeyValue<String,Long> readTextFromStream(InputStream inputStream, long maxSize) throws IOException
inputStream
- the input streammaxSize
- the max size of the loaded data. If equals to -1 the limit
will not be enforced.IOException
- Signals that an I/O exception has occurred.public static File saveFile(ByteArrayOutputStream stream, String name) throws IOException
stream
- the streamname
- the nameIOException
- Signals that an I/O exception has occurred.public static File saveTempFile(ByteArrayOutputStream stream, String name) throws IOException
stream
- the streamname
- the nameIOException
- Signals that an I/O exception has occurred.public static void saveTextFile(String fileName, String text) throws IOException
fileName
- the file nametext
- the text to saveIOException
- Signals that an I/O exception has occurred.public static boolean unzip(String destFolder, String wildCard, boolean deleteZip, String... password) throws Exception
destFolder
- the destination folderwildCard
- the wild carddeleteZip
- if true - delete original zip filepassword
- the passwordException
- in case of any errorpublic static List<String> unzipOrUnGzipFiles(boolean isUnGZip, boolean isFlat, String destFolder, String zipFileName, String... password) throws Exception
isUnGZip
- boolean, if true unzip the gzip fileisFlat
- boolean, if true do not create nested foldersdestFolder
- the destination folder namezipFileName
- the zip file namepassword
- the passwordException
- in case of any errorpublic static List<String> unZipFileWithCommons(String destFolder, String zipFileName, boolean isFlat, List<String> files) throws Exception
destFolder
- the destination folderzipFileName
- the zip file nameisFlat
- boolean if true do not create subfoldersfiles
- the ListException
- in case of any errorpublic static List<String> unzipOrUnGzipFiles(boolean isUnGZip, String destFolder, String zipFileName, String... password) throws Exception
isUnGZip
- boolean, if true unzip the gzip filedestFolder
- the destination folder namezipFileName
- the zip file namepassword
- the passwordException
- in case of any errorpublic static List<String> unzipFiles(String destFolder, String zipFileName, String... password) throws Exception
destFolder
- the destination folder namezipFileName
- the zip file namepassword
- the passwordException
- in case of any errorpublic static String removeWildCard(String name)
name
- the file namepublic static void unzip(String zipFilePath, String destDirectory) throws IOException
zipFilePath
- the zip file pathdestDirectory
- the dest directoryIOException
- Signals that an I/O exception has occurred.public static void writeObject(String fileName, Object object) throws Exception
fileName
- the file nameobject
- the objectException
- in case of any errorpublic static void writeObject(String fileName, Object object, boolean encrypt) throws Exception
fileName
- the file nameobject
- the objectencrypt
- if true encrypts the fileException
- in case of any errorpublic static void writeObjectToJson(String fileName, Object object) throws Exception
fileName
- the file nameobject
- the objectException
- in case of any errorpublic static void writeObjectToJson(String fileName, Object object, boolean encrypt) throws Exception
fileName
- the file nameobject
- the objectencrypt
- if true encrypts the fileException
- in case of any errorpublic static void writeObjectToJson(String fileName, Object object, Key key) throws Exception
fileName
- the file nameobject
- the objectkey
- the encryption keyException
- in case of any errorpublic static boolean zip(String zipFileName, boolean deleteFiles, String password, String encyptionMethod, String... wildCards) throws Exception
zipFileName
- the zip file namedeleteFiles
- if true - delete original filespassword
- the passwordencyptionMethod
- the encyption method (null - default, ASE - ASE)wildCards
- the wild cardsException
- in case of any errorpublic static String executeReplaceMacros(String sourceFileName, String fileName) throws Exception
sourceFileName
- the source file namefileName
- the file nameException
- in case of any errorpublic static String getFileNameUsingAlgoForSuffix(String fileName, String algo)
fileName
- the file namealgo
- the algopublic static String getFileNameWithSuffix(String fileName, String suffix)
fileName
- the file namesuffix
- the suffixpublic static String getFileNameByWildcard(String sourceFileName, String pattern, String fileName, Set<String> alreadyCreated) throws Exception
sourceFileName
- the source file namepattern
- the patternfileName
- the file namealreadyCreated
- the already createdException
- the exceptionpublic static String getNormalizedFolderName(String folder)
folder
- the folderpublic static String getNormalizedFilename(String folder, String fileName)
folder
- the folderfileName
- the file namepublic static String getParentFolderName(String folder)
test/abc -> test/ test/abc/xyz -> test/abc/ csv/ -> scv/
folder
- the folderpublic static String getActualFolderName(String folder)
test/abc -> abc test/abc/ -> abc test/abc/xyz -> xyz csv/ -> scv csv -> scv
folder
- the folderpublic static String getFirstFolderName(String folder)
test / abc -> test / test / abc / xyz -> test
folder
- the folderpublic static TypedKeyValue<String,String> getRootFolderAndTheRest(String folder)
folder
- the folderpublic static String getNameWithoutPrefix(String name, String prefix)
name
- the nameprefix
- the prefixpublic static String addLastSeparator(String path)
path
- the pathpublic static String removeLastSeparator(String path)
path
- the pathpublic static String getCorrectFolder(String folder)
folder
- the folderpublic static String getFileNameWithExtFromOtherFilename(String filename, String filenameWithExt)
filename
- the filenamefilenameWithExt
- the filename with extensionCopyright © 2010-2020 Toolsverse. All Rights Reserved.