public class FileUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
FileUtil.Converter<K,T>
Transform an input object with Type K to an output object with type T
|
static interface |
FileUtil.MapWriter<K,V>
interface for converting an entry of a map to string
|
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
comma |
static long |
ONE_G |
static long |
ONE_GB |
static long |
ONE_K |
static long |
ONE_KB |
static long |
ONE_M |
static long |
ONE_MB |
Modifier and Type | Method and Description |
---|---|
static void |
cleanDirectory(java.io.File dir) |
static void |
cleanDirectory(java.lang.String dirPath) |
static void |
copyDirectory(java.lang.String sourceDir,
java.lang.String targetDir) |
static void |
copyFile(java.io.File source,
java.io.File target)
fast file copy
|
static void |
copyFile(java.lang.String source,
java.lang.String target) |
static void |
deleteDirectory(java.io.File dir) |
static void |
deleteDirectory(java.lang.String dirPath) |
static void |
deleteFile(java.lang.String source) |
static java.lang.Object |
deserialize(java.lang.String filePath) |
static void |
empty(java.lang.String filePath)
empty a file content
|
static boolean |
exist(java.lang.String filePath)
check whether a file exists
|
static java.lang.String |
formatBytes(long size)
Returns a human-readable version of the file size, where the input represents a specific number of bytes.
|
static java.lang.String |
formatSize(long size)
Returns a human-readable version of the file size.
|
static java.lang.String |
getCurrentFolder() |
static java.lang.String |
getCurrentPath() |
static java.io.BufferedReader |
getReader(java.io.File file)
Get reader of a given file.
|
static java.util.List<java.io.BufferedReader> |
getReader(java.lang.String path)
Return the BufferedReader List of files in a specified directory.
|
static java.lang.String |
getResource(java.lang.String filePath)
Get resource path, supporting file and url io path
|
static java.io.BufferedWriter |
getWriter(java.io.File file)
Get writer of a given file.
|
static java.io.BufferedWriter |
getWriter(java.lang.String path)
Get writer of a given path.
|
static java.io.File[] |
listFiles(java.lang.String dirPath)
list all files of a given folder
|
static java.lang.String |
makeDirectory(java.lang.String... dirs)
Construct directory and return directory path
|
static java.lang.String |
makeDirectory(java.lang.String dirPath)
Make directory if it does not exist
|
static java.lang.String |
makeDirPath(java.lang.String... dirs)
Make directory path using the names of directories.
|
static java.lang.String |
makeDirPath(java.lang.String dirPath)
Make directory path: make sure the path is ended with file separator
|
static void |
moveDirectory(java.lang.String sourceDir,
java.lang.String targetDir) |
static void |
moveFile(java.lang.String source,
java.lang.String target) |
static java.util.Map<java.lang.String,java.lang.Double> |
readAsIDMap(java.lang.String filePath)
read a map in the form of
Map<String, Double> . |
static java.util.Map<java.lang.String,java.lang.Double> |
readAsIDMap(java.lang.String filePath,
java.lang.String sep)
read a map in the form of
Map<String, Double> |
static java.util.List<java.lang.String> |
readAsList(java.lang.String filePath)
Read the content of a file and return it as a
List<String> |
static <T> java.util.List<T> |
readAsList(java.lang.String filePath,
FileUtil.Converter<java.lang.String,T> rh) |
static java.util.Map<java.lang.String,java.lang.String> |
readAsMap(java.lang.String filePath) |
static <T,E> java.util.Map<T,E> |
readAsMap(java.lang.String filePath,
FileUtil.Converter<java.lang.String,java.lang.Object[]> rh) |
static java.util.Map<java.lang.String,java.lang.String> |
readAsMap(java.lang.String filePath,
java.lang.String seperator) |
static java.util.Set<java.lang.String> |
readAsSet(java.lang.String filePath) |
static <T> java.util.Set<T> |
readAsSet(java.lang.String filePath,
FileUtil.Converter<java.lang.String,T> rh) |
static java.lang.String |
readAsString(java.lang.String path) |
static java.lang.String |
readAsString(java.lang.String filePath,
int... lines)
Read String from file at specified line numbers, e.g.
|
static java.lang.String |
readAsString(java.lang.String filePath,
java.lang.String... keywords)
Read the content of a file, if keywords are specified, then only lines with these keywords will be read
|
static void |
renameFile(java.io.File file,
java.lang.String regex,
java.lang.String replacement) |
static void |
renameFiles(java.lang.String dirPath,
java.lang.String regex,
java.lang.String replacement)
Rename files in a folder by replacing keywords
|
static void |
serialize(java.lang.Object obj,
java.lang.String filePath) |
static <T> void |
writeList(java.lang.String filePath,
java.util.Collection<T> objs)
Write contents in
Collection<T> to a file. |
static <T> void |
writeList(java.lang.String filePath,
java.util.Collection<T> objs,
boolean append)
Write contents in
Collection<T> to a file. |
static <T> void |
writeList(java.lang.String filePath,
java.util.Collection<T> ts,
FileUtil.Converter<T,java.lang.String> lw,
boolean append)
Write contents in
Collection<T> to a file with the help of a writer helper. |
static <T> void |
writeListSyn(java.lang.String filePath,
java.util.List<T> objs)
Write contents in
Collection<T> to a file. |
static void |
writeString(java.lang.String filePath,
java.lang.String content)
Write a string into a file
|
static void |
writeString(java.lang.String filePath,
java.lang.String content,
boolean append)
Write a string into a file with the given path and content.
|
static <T> void |
writeVector(java.lang.String filePath,
java.util.List<T> objs)
Write contents in
List<T> to a file. |
static <T> void |
writeVector(java.lang.String filePath,
java.util.List<T> ts,
FileUtil.Converter<T,java.lang.String> wh,
boolean append)
Write contents in
List<T> to a file with the help of a writer helper. |
static void |
zipFolder(java.lang.String dirPath,
java.lang.String filePath)
Zip a given folder
|
public static final long ONE_KB
public static final long ONE_K
public static final long ONE_MB
public static final long ONE_M
public static final long ONE_GB
public static final long ONE_G
public static final java.lang.String comma
public static java.lang.String formatBytes(long size)
size
- the number of bytespublic static java.lang.String formatSize(long size)
size
- the size of a file in units (not in bytes)public static java.lang.String getResource(java.lang.String filePath)
filePath
- file pathpublic static java.util.List<java.io.BufferedReader> getReader(java.lang.String path) throws java.io.IOException, java.net.URISyntaxException
path
- The path of the specified directory or file.
Relative and absolute paths are both supported.java.io.IOException
- if I/O error occursjava.net.URISyntaxException
- if URI Syntax error occurspublic static java.io.BufferedReader getReader(java.io.File file) throws java.io.FileNotFoundException
file
- a given filejava.io.FileNotFoundException
- if can't find the filepublic static java.io.BufferedWriter getWriter(java.lang.String path) throws java.lang.Exception
path
- a given pathjava.lang.Exception
- if error occurspublic static java.io.BufferedWriter getWriter(java.io.File file) throws java.lang.Exception
file
- a given filejava.io.FileNotFoundException
- if can't find the filejava.lang.Exception
public static java.lang.String getCurrentFolder()
public static java.lang.String getCurrentPath()
public static java.lang.String makeDirPath(java.lang.String dirPath)
dirPath
- raw directory pathpublic static java.lang.String makeDirPath(java.lang.String... dirs)
dirs
- names of directoriespublic static java.lang.String makeDirectory(java.lang.String dirPath)
dirPath
- a given directory pathpublic static java.lang.String makeDirectory(java.lang.String... dirs)
dirs
- names of directoriespublic static void writeString(java.lang.String filePath, java.lang.String content) throws java.lang.Exception
filePath
- : the name of file to be writtencontent
- : the content of a string to be writtenjava.lang.Exception
- if error occurspublic static void writeString(java.lang.String filePath, java.lang.String content, boolean append) throws java.lang.Exception
filePath
- path of the filecontent
- content to writeappend
- whether appendjava.lang.Exception
- if error occurspublic static <T> void writeList(java.lang.String filePath, java.util.Collection<T> objs) throws java.lang.Exception
Collection<T>
to a file.T
- the type parameterfilePath
- path of the file to writeobjs
- content to writejava.lang.Exception
- if error occurspublic static <T> void writeList(java.lang.String filePath, java.util.Collection<T> objs, boolean append) throws java.lang.Exception
Collection<T>
to a file.T
- the type parameterfilePath
- path of the file to writeobjs
- content to writeappend
- whether to appendjava.lang.Exception
- if error occurspublic static <T> void writeListSyn(java.lang.String filePath, java.util.List<T> objs) throws java.lang.Exception
Collection<T>
to a file.T
- the type parameterfilePath
- path of the file to writeobjs
- content to writejava.lang.Exception
- if error occurspublic static <T> void writeList(java.lang.String filePath, java.util.Collection<T> ts, FileUtil.Converter<T,java.lang.String> lw, boolean append) throws java.lang.Exception
Collection<T>
to a file with the help of a writer helper.T
- the type parameterfilePath
- path of the file to writets
- content to writelw
- writer helperappend
- whether to appendjava.lang.Exception
- if error occurspublic static <T> void writeVector(java.lang.String filePath, java.util.List<T> objs) throws java.lang.Exception
List<T>
to a file.T
- the type parameterfilePath
- path of the file to writeobjs
- content to writejava.lang.Exception
- if error occurspublic static <T> void writeVector(java.lang.String filePath, java.util.List<T> ts, FileUtil.Converter<T,java.lang.String> wh, boolean append) throws java.lang.Exception
List<T>
to a file with the help of a writer helper.T
- the type parameterfilePath
- path of the file to writets
- content to writewh
- writer helperappend
- whether to appendjava.lang.Exception
- if error occurspublic static java.lang.String readAsString(java.lang.String filePath, java.lang.String... keywords) throws java.lang.Exception
filePath
- the file to be readkeywords
- the keywords of lines to be readjava.lang.Exception
- if error occurspublic static java.lang.String readAsString(java.lang.String filePath, int... lines) throws java.lang.Exception
filePath
- file pathlines
- specified line numbersjava.lang.Exception
- if error occurs during readingpublic static java.lang.String readAsString(java.lang.String path) throws java.lang.Exception
java.lang.Exception
public static java.util.List<java.lang.String> readAsList(java.lang.String filePath) throws java.lang.Exception
List<String>
filePath
- : the file to be readjava.util.List<String>
java.lang.Exception
- if error occurs during readingpublic static <T> java.util.List<T> readAsList(java.lang.String filePath, FileUtil.Converter<java.lang.String,T> rh) throws java.io.FileNotFoundException, java.lang.Exception
java.io.FileNotFoundException
java.lang.Exception
public static java.util.Set<java.lang.String> readAsSet(java.lang.String filePath) throws java.io.FileNotFoundException, java.lang.Exception
java.io.FileNotFoundException
java.lang.Exception
public static <T> java.util.Set<T> readAsSet(java.lang.String filePath, FileUtil.Converter<java.lang.String,T> rh) throws java.io.FileNotFoundException, java.lang.Exception
java.io.FileNotFoundException
java.lang.Exception
public static java.util.Map<java.lang.String,java.lang.String> readAsMap(java.lang.String filePath) throws java.io.FileNotFoundException, java.lang.Exception
java.io.FileNotFoundException
java.lang.Exception
public static java.util.Map<java.lang.String,java.lang.String> readAsMap(java.lang.String filePath, java.lang.String seperator) throws java.lang.Exception
java.lang.Exception
public static <T,E> java.util.Map<T,E> readAsMap(java.lang.String filePath, FileUtil.Converter<java.lang.String,java.lang.Object[]> rh) throws java.lang.Exception
java.lang.Exception
public static java.util.Map<java.lang.String,java.lang.Double> readAsIDMap(java.lang.String filePath) throws java.lang.Exception
Map<String, Double>
.filePath
- path of the fileMap<String, Double>
java.lang.Exception
- if error occurs during readingpublic static java.util.Map<java.lang.String,java.lang.Double> readAsIDMap(java.lang.String filePath, java.lang.String sep) throws java.lang.Exception
Map<String, Double>
filePath
- path of the filesep
- sepMap<String, Double>
java.lang.Exception
- if error occurs during readingpublic static void serialize(java.lang.Object obj, java.lang.String filePath) throws java.lang.Exception
java.lang.Exception
public static java.lang.Object deserialize(java.lang.String filePath) throws java.lang.Exception
java.lang.Exception
public static void renameFiles(java.lang.String dirPath, java.lang.String regex, java.lang.String replacement) throws java.lang.Exception
dirPath
- the directory of filesregex
- the old string needed to be replaced, supporting regular expressionreplacement
- the new string used to replace old stringjava.lang.Exception
- if error occurspublic static void renameFile(java.io.File file, java.lang.String regex, java.lang.String replacement)
public static void copyFile(java.lang.String source, java.lang.String target) throws java.lang.Exception
java.lang.Exception
public static void copyFile(java.io.File source, java.io.File target) throws java.lang.Exception
source
- source filetarget
- target filejava.lang.Exception
- if error occurspublic static void deleteFile(java.lang.String source) throws java.lang.Exception
java.lang.Exception
public static void deleteDirectory(java.lang.String dirPath) throws java.lang.Exception
java.lang.Exception
public static void deleteDirectory(java.io.File dir) throws java.lang.Exception
java.lang.Exception
public static void cleanDirectory(java.lang.String dirPath) throws java.lang.Exception
java.lang.Exception
public static void cleanDirectory(java.io.File dir) throws java.lang.Exception
java.lang.Exception
public static void moveFile(java.lang.String source, java.lang.String target) throws java.lang.Exception
java.lang.Exception
public static void moveDirectory(java.lang.String sourceDir, java.lang.String targetDir) throws java.lang.Exception
java.lang.Exception
public static void copyDirectory(java.lang.String sourceDir, java.lang.String targetDir) throws java.lang.Exception
java.lang.Exception
public static void empty(java.lang.String filePath) throws java.lang.Exception
filePath
- file pathjava.lang.Exception
- if error occurspublic static boolean exist(java.lang.String filePath)
filePath
- file pathpublic static java.io.File[] listFiles(java.lang.String dirPath)
dirPath
- a given folderpublic static void zipFolder(java.lang.String dirPath, java.lang.String filePath) throws java.lang.Exception
dirPath
- a given folder: must be all files (not sub-folders)filePath
- zipped filejava.lang.Exception
- if error occursCopyright © 2017. All Rights Reserved.