public class FileUtil
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static int |
consumeStream(java.io.InputStream in)
read all the bytes off the stream, perhaps to empty a URL response.
|
static boolean |
deleteFileTree(java.io.File root)
deletes all files and folders below root, and root, just as "rm -r" would.
|
static boolean |
deleteFileTree(java.io.File root,
java.util.Set<java.lang.String> exclude)
deletes all files and folders below root, and root, just as "rm -r" would, excluding
any files named in exclude.
|
static boolean |
deleteWithinFileTree(java.io.File root,
java.lang.String name)
deletes all files with the given name, and root, just as "find .
|
static boolean |
fileCompare(java.io.File file1,
java.io.File file2)
returns True if the file contents are equal.
|
static void |
fileCopy(java.io.File src,
java.io.File dst)
copies the file or folder from src to dst.
|
static java.io.File |
find(java.io.File[] roots,
java.lang.String name)
find a files with the given name within one of the given roots.
|
static int |
find(java.io.File root,
java.util.regex.Pattern pattern,
java.util.List<java.lang.String> result)
find a files with the given pattern within the given root,
just as "find .
|
static java.io.File |
find(java.io.File root,
java.lang.String name)
find a files with the given name within the given root, just as "find .
|
static java.lang.String |
getMagic(java.io.File src)
return the first four bytes of the file as a string.
|
static boolean |
isParent(java.io.File possibleParent,
java.io.File maybeChild)
return true of the maybeChild parent is a child of possibleParent.
|
static int |
lineCount(java.io.File f)
return the number of lines in the text file.
|
static java.util.List<java.io.File> |
listRecursively(java.io.File root,
java.util.regex.Pattern name,
java.util.List<java.io.File> matches)
find all files under the root matching the spec.
|
static java.io.File[] |
listRecursively(java.io.File root,
java.lang.String glob)
Return an array of files where the regex is found at the end.
|
static java.lang.String |
readFileToString(java.io.File f)
read all the bytes in the UTF-8 encoded file into a string.
|
static java.lang.String |
readInputStreamToString(java.io.InputStream ins)
read all the bytes in the UTF-8 encoded inputStream into a string.
|
static void |
writeStringToFile(java.io.File f,
java.lang.String src)
write all the bytes in the string to a file using UTF-8 encoding.
|
public static boolean fileCompare(java.io.File file1, java.io.File file2)
file1
- file2
- public static boolean isParent(java.io.File possibleParent, java.io.File maybeChild)
possibleParent
- parent file.maybeChild
- a file or folder which may exist within possibleParent.public static boolean deleteFileTree(java.io.File root) throws java.lang.IllegalArgumentException
root
- the root where we start deleting.java.lang.IllegalArgumentException
- if it is unable to delete a filepublic static boolean deleteFileTree(java.io.File root, java.util.Set<java.lang.String> exclude) throws java.lang.IllegalArgumentException
root
- the root where we start deleting.exclude
- null or a set containing names to exclude.java.lang.IllegalArgumentException
- if it is unable to delete a filepublic static boolean deleteWithinFileTree(java.io.File root, java.lang.String name) throws java.lang.IllegalArgumentException
root
- the root directory of the tree.name
- the file name.java.lang.IllegalArgumentException
- if it is unable to delete a filepublic static java.io.File find(java.io.File root, java.lang.String name) throws java.lang.IllegalArgumentException
root
- the root to startname
- name to look for.java.lang.IllegalArgumentException
- if the root does not exist.public static int find(java.io.File root, java.util.regex.Pattern pattern, java.util.List<java.lang.String> result) throws java.lang.IllegalArgumentException
root
- the root to startpattern
- name to look for.result
- results are added to this list, or null if the count that is all that is needed.java.lang.IllegalArgumentException
- if the root does not exist.Glob
public static java.io.File find(java.io.File[] roots, java.lang.String name)
roots
- array of roots to start search.name
- name to look for.public static java.util.List<java.io.File> listRecursively(java.io.File root, java.util.regex.Pattern name, java.util.List<java.io.File> matches)
root
- the root of the search (e.g. /fonts/)name
- the pattern to matchmatches
- list that will accept the matches, or null if one should be created.public static java.io.File[] listRecursively(java.io.File root, java.lang.String glob)
root
- the root of the search (e.g. /fonts/)glob
- the glob to match (e.g. *.ttf)public static void fileCopy(java.io.File src, java.io.File dst) throws java.io.FileNotFoundException, java.io.IOException
src
- the source file or folder.dst
- the location for the new file or folder.java.io.FileNotFoundException
java.io.IOException
public static java.lang.String getMagic(java.io.File src) throws java.io.FileNotFoundException, java.io.IOException
src
- java.io.FileNotFoundException
java.lang.IllegalArgumentException
- if the file is less than four bytes.java.io.IOException
public static java.lang.String readInputStreamToString(java.io.InputStream ins) throws java.io.IOException
ins
- the input streamjava.io.IOException
public static java.lang.String readFileToString(java.io.File f) throws java.io.IOException
f
- the file, which presumed to be UTF-8 (or ASCII) encoded.java.io.IOException
public static void writeStringToFile(java.io.File f, java.lang.String src) throws java.io.IOException
f
- the file name.src
- the string to write to the file.java.io.IOException
public static int lineCount(java.io.File f) throws java.io.FileNotFoundException, java.io.IOException
f
- the filejava.io.FileNotFoundException
java.io.IOException
BufferedReader
public static int consumeStream(java.io.InputStream in) throws java.io.IOException
in
- the input stream, which will not be closed by this method.java.io.IOException