public class FilesUtil extends Object
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ENCODING |
Constructor and Description |
---|
FilesUtil() |
Modifier and Type | Method and Description |
---|---|
static boolean |
copyFile(String srcPath,
String dstPath,
boolean replace)
copy a file
|
static void |
copyFolder(String srcPath,
String dstPath)
copy a folder
|
boolean |
copyTheFile(String fileName,
String srcPath,
String dstPath)
copy a file from srcPath to dstPath
|
static String |
createFolders(String... paths)
create multi-level directory
Example use: createFolders("/", "Users", "Power", "Main"); |
static void |
delAllFile(String folderPath)
delete all files inside folder
|
static boolean |
delFile(String filePathAndName)
delete file
|
static void |
delFolder(String folderPath)
delete folder
|
static List<String> |
getAllFileAbsPath(String path,
boolean isDepth)
get all absolute path file in the folder
|
static List<String> |
getAllFileNames(String path,
boolean isDepth)
get all abs of file in the folder
|
static List<File> |
getAllFiles(String path,
boolean isDepth)
get all files in a folder (Only file)
|
static List<File> |
getAllFiles(String path,
String extension)
|
static List<File> |
getAllFiles(String path,
String extension,
boolean isDepth)
get all file from the folder
|
static String |
getExtension(String fileName)
get file extension/suffix (without dot)
Example: html, txt, pdf, etc. |
static File |
getFileFromRoot(String... more)
get path start from
current project path |
static String |
getFileName(String path)
get file name from path
|
static String |
getNameWithoutExtension(String fileName)
get file name without extension
example: test.txt will return test |
static FileOutputStream |
getOutputStream(String path)
get output file, if file not exist automatic create file and if it's still not exist return
null |
static boolean |
isDirectory(String path)
check is path a directory
|
static boolean |
isDirectoryEmpty(String path)
check is that directory empty or not
|
static boolean |
isDirectoryExist(String path)
check if directory exists, if not exist, create it, return false if create failed
|
static boolean |
isDirectoryExistNotCreate(String path)
check if directory exists
|
static boolean |
isFileExist(String fileNameAndPath) |
static boolean |
isFileExistNotCreate(String fileNameAndPath)
check if the specified file exists
|
static boolean |
moveFile(String srcPath,
String dstPath)
move a file
|
static void |
moveFolder(String srcPath,
String dstPath)
move a folder
|
boolean |
moveTheFile(String fileName,
String srcPath,
String dstPath)
move a file
|
static boolean |
newFile(String filePathAndName,
String fileContent)
create new file
|
static boolean |
newFile(String filePathAndName,
String fileContent,
boolean append)
create file
|
static boolean |
newFile(String filePathAndName,
String fileContent,
String encoding)
create file with specified encoding
|
static boolean |
newFolder(String folderPath)
create folder
|
static String |
readAll(File file)
read all context in file
File |
static String |
readAll(String filePathAndName)
read text file content, return string split by "\n"
|
static String |
readAll(String filePathAndName,
String encoding)
read text file content, return string split by "\n"
|
static String |
readLine(String filePathAndName,
long rowIndex)
read the specified line content of text file
|
static List<String> |
readLine(String filePathAndName,
String encoding)
read the line content of text file, in array
PS. size of element = number of line |
static String[] |
separatePath(String path)
separate the path to file name array
example: /var/private/zz/yy - [var, private, zz, yy] |
public static String DEFAULT_ENCODING
public static String readAll(File file)
File
file
- reading filepublic static String readAll(String filePathAndName)
filePathAndName
- String file name with absolute pathpublic static String readAll(String filePathAndName, String encoding)
filePathAndName
- String file name with absolute pathencoding
- String file encodingpublic static List<String> readLine(String filePathAndName, String encoding)
filePathAndName
- String file name with absolute pathencoding
- String file encodingpublic static String readLine(String filePathAndName, long rowIndex)
filePathAndName
- String file name with absolute pathrowIndex
- the row number that want to readpublic static boolean newFile(String filePathAndName, String fileContent)
filePathAndName
- String file path and namefileContent
- String file contentpublic static boolean newFile(String filePathAndName, String fileContent, boolean append)
filePathAndName
- String file path and namefileContent
- String file contentappend
- true to append, false to createpublic static boolean newFile(String filePathAndName, String fileContent, String encoding)
filePathAndName
- String file path and namefileContent
- String file contentencoding
- the specified encoding, such as GBK or UTF-8public static boolean delFile(String filePathAndName)
filePathAndName
- String file path and namepublic static void delAllFile(String folderPath)
folderPath
- String folder Pathpublic static boolean newFolder(String folderPath)
folderPath
- String folder pathpublic static void delFolder(String folderPath)
folderPath
- String folder pathpublic static boolean copyFile(String srcPath, String dstPath, boolean replace)
srcPath
- String the source path (File)dstPath
- String the destination path (File)replace
- is dstPath exist, and this is true that file will be replaced with new contentpublic static void copyFolder(String srcPath, String dstPath)
srcPath
- String the source pathdstPath
- String the destination pathpublic static boolean moveFile(String srcPath, String dstPath)
srcPath
- String the source pathdstPath
- String the destination pathpublic static void moveFolder(String srcPath, String dstPath)
srcPath
- String the source pathdstPath
- String the destination pathpublic static String createFolders(String... paths)
paths
- must be at least 1 element is root folder
(Optional) the folder that want to created.public static File getFileFromRoot(String... more)
current project path
more
- addition string to be joined to start pathFile
Paths.get(String, String...)
public static List<File> getAllFiles(String path, boolean isDepth)
path
- String folder pathisDepth
- true is need to scan all subdirectoriespublic static List<File> getAllFiles(String path, String extension)
path
- folder pathextension
- file extensionpublic static List<File> getAllFiles(String path, String extension, boolean isDepth)
path
- folder pathextension
- filter file extensionisDepth
- recursive reading sub folderpublic static List<String> getAllFileAbsPath(String path, boolean isDepth)
public static List<String> getAllFileNames(String path, boolean isDepth)
public static String getExtension(String fileName)
fileName
- file namepublic static String getNameWithoutExtension(String fileName)
test.txt
will return test
fileName
- file name or file name with pathpublic static String getFileName(String path)
path
- absolute file/folder pathpublic static boolean isFileExist(String fileNameAndPath)
public static boolean isFileExistNotCreate(String fileNameAndPath)
fileNameAndPath
- the name of the file to be checkedpublic static boolean isDirectoryExist(String path)
path
- folder pathpublic static boolean isDirectoryExistNotCreate(String path)
path
- folder pathpublic static boolean isDirectory(String path)
path
- absolute folder pathpublic static boolean isDirectoryEmpty(String path)
path
- absolute folder pathpublic boolean copyTheFile(String fileName, String srcPath, String dstPath)
fileName
- file name (File)srcPath
- source path (Folder)dstPath
- destination path (Folder)public boolean moveTheFile(String fileName, String srcPath, String dstPath)
fileName
- file name (File)srcPath
- source path (Folder)dstPath
- destination path (Folder)public static String[] separatePath(String path)
/var/private/zz/yy
- [var, private, zz, yy]path
- file name with absolute path
public static FileOutputStream getOutputStream(String path)
null
path
- file name with absolute pathCopyright © 2017. All rights reserved.