public class FileUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
FileUtil.FileIterable |
static class |
FileUtil.FileIterator |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
EXTN_CHR |
static java.lang.String |
GZIP_EXTN |
Constructor and Description |
---|
FileUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
appendText(java.lang.String content,
java.io.File outFile)
Appends text in an existing file.
|
static void |
appendText(java.lang.String content,
java.lang.String outFileName)
Appends text in an existing file.
|
static java.lang.String |
bytesToString(byte[] bs) |
static void |
checkReadableOrThrowError(java.lang.String description,
java.lang.String path)
Ensure that a path exists and is readable, otherwise throw a RuntimeException using a
description of the path.
|
static void |
closeOutputStream(java.io.OutputStream os) |
static java.lang.String |
ensureSlash(java.lang.String dirPath)
if the dirPath ends in a '/' it does nothing otherwise it adds a '/'
|
static void |
ensureWorldReadable(java.io.File f) |
static void |
ensureWriteable(java.io.File f)
creates any directories that need to exist to create and write the file should not create the
file
|
static boolean |
exists(java.lang.String path)
Return true if the file exists
|
static java.io.InputStream |
fastStream(java.io.File file)
get an input stream for the file that is fast, trading memory for speed
|
static java.lang.String |
getCanonicalPath(java.io.File file)
gets the canonical path for the file, or null if it cannot
|
static java.lang.String |
getCanonicalPath(java.lang.String path) |
static java.io.DataInputStream |
getDataInput(java.lang.String filename) |
static java.io.DataOutputStream |
getDataOutput(java.lang.String filename) |
static java.lang.String |
getExtension(java.lang.String fileName)
return the last extension from a filename, including the '.'
|
static java.io.PrintStream |
getFilePrintStream(java.lang.String filename)
constructs PrintStream to write to the file called filename, creating any parent directories, and gzipping the output if the filename ends with ".gz"
|
static java.lang.Iterable<java.lang.String> |
getLines(java.lang.String filename) |
static java.lang.Iterable<java.lang.String> |
getRawLines(java.lang.String filename) |
static <T> T |
loadObjectFromFile(java.io.File file)
assume the file contains exactly one serialized object, read it and return it if the file
doesn't exist, they return null
|
static <T> T |
loadObjectFromFile(java.lang.String fileName)
Given a fileName, assume the file contains exactly one serialized object, read it and return it
if the file doesn't exist, they return null
|
static java.lang.String |
readFileAsString(java.io.File file)
read the contents of the file into a string and return it if the file doesn't exist, they
return null = * Based on
https://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html (APACHE LICENSE)
|
static java.lang.String |
readFileAsString(java.lang.String filename)
read the contents of the file into a string and return it
|
static java.lang.String |
readProcessAsString(java.lang.Process proc)
read the stdout of the process into a string and return when the function returns the process
has completed the stderr of the process is ignored
|
static java.lang.String |
readResourceAsString(java.lang.String resource) |
static java.lang.String |
readStreamAsString(java.io.InputStream is)
reads the input stream into a String and closes it
|
static java.lang.String |
removeExtension(java.lang.String fileName)
remove the last extension from a file If it is a directory, then return the name If it is a
hidden file without extn, return the name If it is a hidden file with extn, return name sans
extn If is a regular file with extn, return the name sans extn
|
static java.lang.String |
removeExtensions(java.lang.String file)
removes the '.whatever.txt.gz' at the end of the file.
|
static void |
saveObjectAsFile(java.io.File file,
java.lang.Object object)
save the presumably Serializable object to the file
|
static void |
saveObjectAsFile(java.lang.String fileName,
java.lang.Object object)
save the presumably Serializable object to the file, given a fileName
|
static byte[] |
stringToBytes(java.lang.String str) |
static void |
writeFileAsString(java.io.File file,
java.lang.String text)
writes the text to the file wither compressed or uncompressed
|
static void |
writeFileAsString(java.lang.String filename,
java.lang.String text)
writes the text to the file, given a fileName and text
|
public static java.lang.String GZIP_EXTN
public static java.lang.String EXTN_CHR
public static void writeFileAsString(java.io.File file, java.lang.String text)
file
- text
- public static void writeFileAsString(java.lang.String filename, java.lang.String text)
filename
- text
- public static void ensureWriteable(java.io.File f)
public static java.lang.String readFileAsString(java.lang.String filename)
filename
- public static java.lang.String readFileAsString(java.io.File file)
file
- public static java.lang.String readStreamAsString(java.io.InputStream is)
is
- public static void saveObjectAsFile(java.io.File file, java.lang.Object object)
file
- object
- public static void saveObjectAsFile(java.lang.String fileName, java.lang.Object object)
file
- object
- public static <T> T loadObjectFromFile(java.io.File file)
file
- public static <T> T loadObjectFromFile(java.lang.String fileName)
file
- public static java.lang.String removeExtension(java.lang.String fileName)
fileName
- public static java.lang.String removeExtensions(java.lang.String file)
file
- public static java.lang.String getExtension(java.lang.String fileName)
fileName
- public static java.lang.String ensureSlash(java.lang.String dirPath)
dirPath
- public static boolean exists(java.lang.String path)
path
- public static java.lang.String getCanonicalPath(java.io.File file)
file
- public static java.io.InputStream fastStream(java.io.File file)
file
- public static java.lang.String readProcessAsString(java.lang.Process proc) throws java.lang.InterruptedException
proc
- java.lang.InterruptedException
public static java.io.PrintStream getFilePrintStream(java.lang.String filename)
filename
- public static java.lang.Iterable<java.lang.String> getRawLines(java.lang.String filename)
public static java.lang.Iterable<java.lang.String> getLines(java.lang.String filename)
public static java.lang.String readResourceAsString(java.lang.String resource)
public static java.io.DataOutputStream getDataOutput(java.lang.String filename)
public static java.io.DataInputStream getDataInput(java.lang.String filename)
public static void ensureWorldReadable(java.io.File f)
public static void closeOutputStream(java.io.OutputStream os)
public static byte[] stringToBytes(java.lang.String str)
public static java.lang.String bytesToString(byte[] bs)
public static void checkReadableOrThrowError(java.lang.String description, java.lang.String path)
public static java.lang.String getCanonicalPath(java.lang.String path)
public static void appendText(java.lang.String content, java.lang.String outFileName) throws java.io.IOException
content
- outFile
- java.io.IOException
public static void appendText(java.lang.String content, java.io.File outFile) throws java.io.IOException
content
- outFile
- java.io.IOException