Package com.xxl.tool.io
Class FileTool
java.lang.Object
com.xxl.tool.io.FileTool
File Tool
- Author:
- xuxueli 2024-10-27
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanclean directorystatic booleanclean directorystatic booleanclean directorystatic Filecopy filestatic Filecopy filestatic Filecopy filestatic Filecopy(Path src, Path dest, StandardCopyOption... options) copy file 1、source must exists 2、source only support file (todo: support directory) 3、destination file can not be same as source file 4、destination can not be sub of sourcestatic booleancreateDirectories(File dir) create directories 1、will create all necessary parent directoriesstatic FilecreateFile(File file) create actual file on diskstatic FilecreateFile(String path) create actual file on diskstatic FilecreateFile(String parent, String child) create actual file on diskstatic booleancreateParentDirectories(File file) create parent directories of file 1、will create all necessary parent directoriesstatic booleandelete file or directorystatic booleandelete file or directorystatic booleanwhether a file existsstatic booleanis file existstatic Filecreate new file object, not actual file on diskstatic Filecreate new file object, not actual file on diskstatic booleanisDirectory(File file) is directorystatic booleanisDirectory(String path) is directorystatic booleanis empty file or directorystatic booleanis filestatic booleanis filestatic booleanisNotEmpty(File file) is not empty, file or directorystatic booleanisSameFile(File file1, File file2) is same filestatic booleanis sub file of parentstatic Filemove file or directorystatic Filemove filestatic Filemove file or directorystatic Filemove(Path src, Path dest, StandardCopyOption... options) move file or directory 1、source must exists 2、destination file can not be same as source file 3、destination can not be sub of sourceread linesread linesstatic voidread lines with consumerstatic voidread lines with consumerstatic StringreadString(String path) read stringstatic StringreadString(String path, Charset charset) read stringstatic Filerename filestatic longget file size, not include directory sizestatic longget file sizestatic longget file size, not include directory sizestatic PathtoAbsoluteNormal(Path path) to absolute normal pathstatic PathtoAbsolutePath(Path path) to absolute pathstatic inttotalLines(File file) get total linesstatic inttotalLines(File file, int bufferSize, boolean lastLineSeparatorAsLine) get total linesstatic inttotalLines(String path) get total linesstatic voidwriteLines(String path, Iterable<?> lines) write lines 1、default appendstatic voidwriteLines(String path, Iterable<?> lines, boolean append) write linesstatic voidwriteLines(String path, Iterable<?> lines, String lineSeparator, boolean append, Charset charset) write linesstatic voidwriteLines(String path, Supplier<?> lineSupplier) write lines 1、default appendstatic voidwriteLines(String path, Supplier<?> lineSupplier, boolean append) write linesstatic voidwriteLines(String path, Supplier<?> lineSupplier, String lineSeparator, boolean append, Charset charset) write linesstatic voidwriteString(String path, String content) write stringstatic voidwriteString(String path, String content, boolean append) write stringstatic voidwriteString(String path, String content, boolean append, Charset charset) write string
-
Constructor Details
-
FileTool
public FileTool()
-
-
Method Details
-
file
create new file object, not actual file on disk- Parameters:
path- file path- Returns:
- file object
-
file
create new file object, not actual file on disk- Parameters:
parent- parent pathchild- child path- Returns:
- file object
-
createFile
create actual file on disk- Parameters:
path- file path- Returns:
- file
- Throws:
IOException
-
createFile
create actual file on disk- Parameters:
parent- parent pathchild- child path- Returns:
- file
- Throws:
IOException
-
createFile
create actual file on disk- Parameters:
file- file object- Returns:
- file
- Throws:
IOException
-
createParentDirectories
create parent directories of file 1、will create all necessary parent directories- Parameters:
file- file object- Throws:
IOException
-
createDirectories
create directories 1、will create all necessary parent directories- Parameters:
dir- dir- Returns:
- true if and only if the directory was created, along with all necessary parent directories; false otherwise
- Throws:
IOException
-
isDirectory
is directory- Parameters:
path- path of file- Returns:
trueif is directory
-
isDirectory
is directory- Parameters:
file- file- Returns:
trueif is directory
-
isFile
is file- Parameters:
path- path of file- Returns:
trueif is file
-
isFile
is file- Parameters:
file- file object- Returns:
trueif is file
-
isEmpty
is empty file or directory- Parameters:
file- file object- Returns:
trueif empty
-
isNotEmpty
is not empty, file or directory- Parameters:
file- file object- Returns:
trueif is not empty
-
isSameFile
is same file- Parameters:
file1- file1 objectfile2- file2 object- Returns:
- true if is same file
- Throws:
IOException
-
isSub
is sub file of parent- Parameters:
parent- parent filesub- sub file- Returns:
- true if is sub file of parent
-
exists
is file exist- Parameters:
path- path of file- Returns:
- true if file exist
-
exists
whether a file exists- Parameters:
file- file object- Returns:
- true if file exist
-
toAbsoluteNormal
to absolute normal path- Parameters:
path- file path- Returns:
- absolute normal path
-
toAbsolutePath
to absolute path- Parameters:
path- file path- Returns:
- absolute path
-
size
get file size, not include directory size- Parameters:
path- file path- Returns:
- size in bytes
-
size
get file size, not include directory size- Parameters:
file- file object- Returns:
- size in bytes
-
size
get file size- Parameters:
file- file objectincludeDirSize- include directory size- Returns:
- size in bytes
-
totalLines
get total lines- Parameters:
path- path of file- Returns:
- total line number
-
totalLines
get total lines- Parameters:
file- file object- Returns:
- total line number
-
totalLines
get total lines- Parameters:
file- filebufferSize- buffer sizelastLineSeparatorAsLine- last line separator as line- Returns:
- total line number
-
delete
delete file or directory- Parameters:
path- path of file- Returns:
- true if success
-
delete
delete file or directory- Parameters:
file- file object- Returns:
- true if success
-
clean
clean directory- Parameters:
dirPath- directory path- Returns:
- true if success
-
clean
clean directory- Parameters:
directory- directory file object- Returns:
- true if success
-
clean
clean directory- Parameters:
directory- directory file objectskipDeleteFailFile- true, skip delete fail file- Returns:
-
copy
copy file- Parameters:
src- source file pathdest- destination file path or directory pathisOverride- true will override, false and dest-file exists will throw FileAlreadyExistsException- Returns:
- destination file
- Throws:
IOException
-
copy
copy file- Parameters:
src- source filedest- destination file or directoryisOverride- true will override, false and dest-file exists will throw FileAlreadyExistsException- Returns:
- destination file
- Throws:
IOException
-
copy
copy file- Parameters:
src- source filedest- destination file or directoryisOverride- true will override, false and dest-file exists will throw FileAlreadyExistsException- Returns:
- destination file
- Throws:
IOException
-
copy
copy file 1、source must exists 2、source only support file (todo: support directory) 3、destination file can not be same as source file 4、destination can not be sub of source- Parameters:
src- source file pathdest- destination file path or directory pathoptions- copy options, such as StandardCopyOption.REPLACE_EXISTING、StandardCopyOption.COPY_ATTRIBUTES- Returns:
- destination file
- Throws:
IOException
-
move
move file- Parameters:
src- source file pathdest- destination file path or directory pathisOverride- true will override, false and dest-file exists will throw FileAlreadyExistsException- Returns:
- destination file
- Throws:
IOException
-
move
move file or directory- Parameters:
src- source filedest- destination file or directoryisOverride- true will override, false and dest-file exists will throw FileAlreadyExistsException- Returns:
- destination file
- Throws:
IOException
-
move
move file or directory- Parameters:
src- source filedest- destination file or directoryisOverride- true will override, false and dest-file exists will throw FileAlreadyExistsException- Returns:
- destination file
- Throws:
IOException
-
move
move file or directory 1、source must exists 2、destination file can not be same as source file 3、destination can not be sub of source- Parameters:
src- source file pathdest- destination file path or directory pathoptions- copy options, such as StandardCopyOption.REPLACE_EXISTING、StandardCopyOption.COPY_ATTRIBUTES- Returns:
- destination file
- Throws:
IOException
-
rename
rename file- Parameters:
file- origin filenewFileName- new file nameisOverride- true if override, false will skip- Returns:
- new file
- Throws:
IOException
-
writeString
write string- Parameters:
path- file pathcontent- file content- Throws:
IOException
-
writeString
write string- Parameters:
path- file pathcontent- file contentappend- true if append, false will override- Throws:
IOException
-
writeString
public static void writeString(String path, String content, boolean append, Charset charset) throws IOException write string- Parameters:
path- file pathcontent- file contentappend- true if append, false will overridecharset- charset encoding- Throws:
IOException
-
writeLines
write lines 1、default append- Parameters:
path- file pathlines- lines content- Throws:
IOException- exception
-
writeLines
write lines- Parameters:
path- file pathlines- lines contentappend- true if append, false will override- Throws:
IOException- exception
-
writeLines
public static void writeLines(String path, Iterable<?> lines, String lineSeparator, boolean append, Charset charset) throws IOException write lines- Parameters:
path- file pathlines- lines contentlineSeparator- line separatorappend- true if append, false will overridecharset- charset encoding- Throws:
IOException- exception
-
writeLines
write lines 1、default append- Parameters:
path- file pathlineSupplier- line supplier- Throws:
IOException- exception
-
writeLines
public static void writeLines(String path, Supplier<?> lineSupplier, boolean append) throws IOException write lines- Parameters:
path- file pathlineSupplier- line supplierappend- true if append, false will override- Throws:
IOException- exception
-
writeLines
public static void writeLines(String path, Supplier<?> lineSupplier, String lineSeparator, boolean append, Charset charset) throws IOException write lines- Parameters:
path- file pathlineSupplier- line supplierlineSeparator- line separatorappend- true if append, false will overridecharset- charset encoding- Throws:
IOException- exception
-
readString
read string- Parameters:
path- file path- Returns:
- file data string
- Throws:
IOException
-
readString
read string- Parameters:
path- file pathcharset- charset encoding- Returns:
- file data string
- Throws:
IOException
-
readLines
read lines- Parameters:
path- file path- Returns:
- file data lines
- Throws:
IOException- exception
-
readLines
read lines- Parameters:
path- file pathcharset- charset encoding- Returns:
- file data lines
- Throws:
IOException- exception
-
readLines
read lines with consumer- Parameters:
path- file pathlineConsumer- line consumer- Throws:
IOException- exception
-
readLines
public static void readLines(String path, Charset charset, Consumer<String> lineConsumer) throws IOException read lines with consumer- Parameters:
path- file pathcharset- charset encodinglineConsumer- line consumer- Throws:
IOException- exception
-