public class FileUtils
extends java.lang.Object
#readFile(String)
read file#readFileToList(String)
read file to string listwriteFile(String, String, boolean)
write filewriteFile(String, InputStream)
write filewriteFile(String, InputStream, boolean)
write filewriteFile(File, InputStream)
write filewriteFile(File, InputStream, boolean)
write fileModifier and Type | Field and Description |
---|---|
static java.lang.String |
FILE_EXTENSION_SEPARATOR |
Constructor and Description |
---|
FileUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
copyFile(java.lang.String sourceFilePath,
java.lang.String destFilePath)
copy file
|
static boolean |
deleteFile(java.lang.String path)
delete file or directory
if path is null or empty, return true
if path not exist, return true
if path exist, delete recursion. return true
|
static java.lang.String |
getFileExtension(java.lang.String filePath)
get suffix of file from path
getFileExtension(null) = ""
getFileExtension("") = ""
getFileExtension(" ") = " "
getFileExtension("a.mp3") = "mp3"
getFileExtension("a.b.rmvb") = "rmvb"
getFileExtension("abc") = ""
getFileExtension("c:\\") = ""
getFileExtension("c:\\a") = ""
getFileExtension("c:\\a.b") = "b"
getFileExtension("c:a.txt\\a") = ""
getFileExtension("/home/admin") = ""
getFileExtension("/home/admin/a.txt/b") = ""
getFileExtension("/home/admin/a.txt/b.mp3") = "mp3"
|
static java.lang.String |
getFileName(java.lang.String filePath)
get file name from path, include suffix
getFileName(null) = null
getFileName("") = ""
getFileName(" ") = " "
getFileName("a.mp3") = "a.mp3"
getFileName("a.b.rmvb") = "a.b.rmvb"
getFileName("abc") = "abc"
getFileName("c:\\") = ""
getFileName("c:\\a") = "a"
getFileName("c:\\a.b") = "a.b"
getFileName("c:a.txt\\a") = "a"
getFileName("/home/admin") = "admin"
getFileName("/home/admin/a.txt/b.mp3") = "b.mp3"
|
static java.lang.String |
getFileNameWithoutExtension(java.lang.String filePath)
get file name from path, not include suffix
getFileNameWithoutExtension(null) = null
getFileNameWithoutExtension("") = ""
getFileNameWithoutExtension(" ") = " "
getFileNameWithoutExtension("abc") = "abc"
getFileNameWithoutExtension("a.mp3") = "a"
getFileNameWithoutExtension("a.b.rmvb") = "a.b"
getFileNameWithoutExtension("c:\\") = ""
getFileNameWithoutExtension("c:\\a") = "a"
getFileNameWithoutExtension("c:\\a.b") = "a"
getFileNameWithoutExtension("c:a.txt\\a") = "a"
getFileNameWithoutExtension("/home/admin") = "admin"
getFileNameWithoutExtension("/home/admin/a.txt/b.mp3") = "b"
|
static long |
getFileSize(java.lang.String path)
get file size
if path is null or empty, return -1
if path exist and it is a file, return file size, else return -1
|
static java.lang.String |
getFolderName(java.lang.String filePath)
get folder name from path
getFolderName(null) = null
getFolderName("") = ""
getFolderName(" ") = ""
getFolderName("a.mp3") = ""
getFolderName("a.b.rmvb") = ""
getFolderName("abc") = ""
getFolderName("c:\\") = "c:"
getFolderName("c:\\a") = "c:"
getFolderName("c:\\a.b") = "c:"
getFolderName("c:a.txt\\a") = "c:a.txt"
getFolderName("c:a\\b\\c\\d.txt") = "c:a\\b\\c"
getFolderName("/home/admin") = "/home"
getFolderName("/home/admin/a.txt/b.mp3") = "/home/admin/a.txt"
|
static boolean |
isFileExist(java.lang.String filePath)
Indicates if this file represents a file on the underlying file system.
|
static boolean |
isFolderExist(java.lang.String directoryPath)
Indicates if this file represents a directory on the underlying file system.
|
static boolean |
makeDirs(java.lang.String filePath)
Creates the directory named by the trailing filename of this file, including the complete directory path required
to create this directory.
|
static boolean |
makeFolders(java.lang.String filePath) |
static java.lang.StringBuilder |
readFile(java.lang.String filePath,
java.lang.String charsetName)
read file
|
static java.util.List<java.lang.String> |
readFileToList(java.lang.String filePath,
java.lang.String charsetName)
read file to string list, a element of list is a line
|
static boolean |
writeFile(java.io.File file,
java.io.InputStream stream)
write file
|
static boolean |
writeFile(java.io.File file,
java.io.InputStream stream,
boolean append)
write file
|
static boolean |
writeFile(java.lang.String filePath,
java.io.InputStream stream)
write file
|
static boolean |
writeFile(java.lang.String filePath,
java.io.InputStream stream,
boolean append)
write file
|
static boolean |
writeFile(java.lang.String filePath,
java.lang.String content,
boolean append)
write file
|
public static final java.lang.String FILE_EXTENSION_SEPARATOR
public static java.lang.StringBuilder readFile(java.lang.String filePath, java.lang.String charsetName)
filePath
- charsetName
- The name of a supported
charset
java.lang.RuntimeException
- if an error occurs while operator BufferedReaderpublic static boolean writeFile(java.lang.String filePath, java.lang.String content, boolean append)
filePath
- content
- append
- is append, if true, write to the end of file, else clear content of file and write into itjava.lang.RuntimeException
- if an error occurs while operator FileWriterpublic static boolean writeFile(java.lang.String filePath, java.io.InputStream stream)
filePath
- stream
- #writeFile(String, InputStream, boolean)}
public static boolean writeFile(java.lang.String filePath, java.io.InputStream stream, boolean append)
file
- the file to be opened for writing.stream
- the input streamappend
- if true
, then bytes will be written to the end of the file rather than the beginningjava.lang.RuntimeException
- if an error occurs while operator FileOutputStreampublic static boolean writeFile(java.io.File file, java.io.InputStream stream)
file
- stream
- #writeFile(File, InputStream, boolean)}
public static boolean writeFile(java.io.File file, java.io.InputStream stream, boolean append)
file
- the file to be opened for writing.stream
- the input streamappend
- if true
, then bytes will be written to the end of the file rather than the beginningjava.lang.RuntimeException
- if an error occurs while operator FileOutputStreampublic static boolean copyFile(java.lang.String sourceFilePath, java.lang.String destFilePath)
sourceFilePath
- destFilePath
- java.lang.RuntimeException
- if an error occurs while operator FileOutputStreampublic static java.util.List<java.lang.String> readFileToList(java.lang.String filePath, java.lang.String charsetName)
filePath
- charsetName
- The name of a supported
charset
java.lang.RuntimeException
- if an error occurs while operator BufferedReaderpublic static java.lang.String getFileNameWithoutExtension(java.lang.String filePath)
getFileNameWithoutExtension(null) = null getFileNameWithoutExtension("") = "" getFileNameWithoutExtension(" ") = " " getFileNameWithoutExtension("abc") = "abc" getFileNameWithoutExtension("a.mp3") = "a" getFileNameWithoutExtension("a.b.rmvb") = "a.b" getFileNameWithoutExtension("c:\\") = "" getFileNameWithoutExtension("c:\\a") = "a" getFileNameWithoutExtension("c:\\a.b") = "a" getFileNameWithoutExtension("c:a.txt\\a") = "a" getFileNameWithoutExtension("/home/admin") = "admin" getFileNameWithoutExtension("/home/admin/a.txt/b.mp3") = "b"
filePath
-
public static java.lang.String getFileName(java.lang.String filePath)
getFileName(null) = null getFileName("") = "" getFileName(" ") = " " getFileName("a.mp3") = "a.mp3" getFileName("a.b.rmvb") = "a.b.rmvb" getFileName("abc") = "abc" getFileName("c:\\") = "" getFileName("c:\\a") = "a" getFileName("c:\\a.b") = "a.b" getFileName("c:a.txt\\a") = "a" getFileName("/home/admin") = "admin" getFileName("/home/admin/a.txt/b.mp3") = "b.mp3"
filePath
- public static java.lang.String getFolderName(java.lang.String filePath)
getFolderName(null) = null getFolderName("") = "" getFolderName(" ") = "" getFolderName("a.mp3") = "" getFolderName("a.b.rmvb") = "" getFolderName("abc") = "" getFolderName("c:\\") = "c:" getFolderName("c:\\a") = "c:" getFolderName("c:\\a.b") = "c:" getFolderName("c:a.txt\\a") = "c:a.txt" getFolderName("c:a\\b\\c\\d.txt") = "c:a\\b\\c" getFolderName("/home/admin") = "/home" getFolderName("/home/admin/a.txt/b.mp3") = "/home/admin/a.txt"
filePath
- public static java.lang.String getFileExtension(java.lang.String filePath)
getFileExtension(null) = "" getFileExtension("") = "" getFileExtension(" ") = " " getFileExtension("a.mp3") = "mp3" getFileExtension("a.b.rmvb") = "rmvb" getFileExtension("abc") = "" getFileExtension("c:\\") = "" getFileExtension("c:\\a") = "" getFileExtension("c:\\a.b") = "b" getFileExtension("c:a.txt\\a") = "" getFileExtension("/home/admin") = "" getFileExtension("/home/admin/a.txt/b") = "" getFileExtension("/home/admin/a.txt/b.mp3") = "mp3"
filePath
- public static boolean makeDirs(java.lang.String filePath)
filePath
- getFolderName(String)
return null, return falsejava.io.File#makeFolder
public static boolean makeFolders(java.lang.String filePath)
filePath
- makeDirs(String)
public static boolean isFileExist(java.lang.String filePath)
filePath
- public static boolean isFolderExist(java.lang.String directoryPath)
directoryPath
- public static boolean deleteFile(java.lang.String path)
path
- public static long getFileSize(java.lang.String path)
path
-