public class Files
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static void |
appendToTextFile(java.lang.String[] lines,
java.lang.String file) |
static java.lang.String |
buildPath(java.lang.String... parts) |
static long |
computeFileChecksum(java.io.File file,
java.util.zip.Checksum checksum) |
static boolean |
delete(java.lang.String file)
Deletes file or recursively deletes a directory
NOTE: this returns true if the file was actually deleted, and false for 2 cases:
1.
|
static boolean |
deleteOrDie(java.lang.String file)
Deletes file or recursively deletes a directory
|
static java.io.BufferedReader |
getBufferedReaderForUtf8(java.lang.String file) |
static java.lang.String |
getCanonicalDirectoryName(java.lang.String path)
Gets the directory name of the canonical path -- the last element in the result of
getCanonicalPath(String). |
static java.lang.String |
getCanonicalPath(java.lang.String path)
Use this function instead of
File.getCanonicalPath(), as that method
can fail during symlink changes, and this method will retry up to 3 times with a
short delay. |
static java.lang.String |
getFileHash(java.lang.String file,
java.lang.String algorithm) |
static java.lang.String |
getTempDirectory(java.lang.String prefix,
java.lang.String suffix) |
static java.lang.String |
getTempDirectory(java.lang.String prefix,
java.lang.String suffix,
java.io.File directory) |
static java.lang.String |
getTempDirectory(java.lang.String prefix,
java.lang.String suffix,
java.lang.String directory) |
static byte[] |
loadFileAsByteArray(java.lang.String file) |
static java.io.Writer |
newBufferedUTF8FileWriter(java.io.File file)
Use this instead of
FileWriter because you cannot specify the character encoding with that. |
static java.io.Writer |
newBufferedUTF8FileWriter(java.lang.String file)
Use this instead of
FileWriter because you cannot specify the character encoding with that. |
static java.util.List<java.lang.String> |
readCommentedTextFile(java.lang.String file)
Reads all the lines in the given file, truncating everything that happens after # (including the #)
|
static float[] |
readFloatsFromTextFile(java.lang.String file) |
static int[] |
readIntsFromTextFile(java.lang.String file) |
static java.lang.Object |
readObjectFromClasspathDir(java.lang.String file)
Convenience for
readObjectFromFile(String file, Class clazz, boolean printException)
where:
clazz is Object.class
printException is false
|
static <T> T |
readObjectFromClasspathDir(java.lang.String file,
java.lang.Class<T> clazz,
boolean printException) |
static java.lang.Object |
readObjectFromFile(java.lang.String file)
Convenience for
readObjectFromFile(String file, Class clazz, boolean printException)
where:
clazz is Object.class
printException is false
|
static java.lang.Object |
readObjectFromFile(java.lang.String file,
boolean printException)
Convenience for
readObjectFromFile(String file, Class clazz, boolean printException)
where:
clazz is Object.class
|
static <T> T |
readObjectFromFile(java.lang.String file,
java.lang.Class<T> clazz)
Convenience for
readObjectFromFile(String file, Class clazz, boolean printException)
where:
printException is false
|
static <T> T |
readObjectFromFile(java.lang.String file,
java.lang.Class<T> clazz,
boolean printException)
Reads an object of type
T from file. |
static java.lang.String |
readStreamAsString(java.io.InputStream in) |
static java.lang.String |
readStreamAsString(java.io.InputStream in,
int bufferSize) |
static java.lang.String[] |
readTextFile(java.lang.String file) |
static java.lang.String[] |
readTextFileOrDie(java.lang.String file) |
static java.lang.String |
toHex(byte[] bytes)
Converts a byte array to a hex string.
|
static boolean |
writeObjectIfChanged(java.lang.Object obj,
java.lang.String filepath)
Deprecated.
|
static boolean |
writeObjectIfChangedOrDie(java.lang.Object obj,
java.lang.String file)
Writes an object to a file only if it is different from the current
contents of the file, or if the file does not exist.
|
static boolean |
writeObjectIfChangedOrDie(java.lang.Object obj,
java.lang.String file,
org.apache.log4j.Logger log)
Writes an object to a file only if it is different from the current
contents of the file, or if the file does not exist.
|
static boolean |
writeObjectToFile(java.lang.Object obj,
java.lang.String file)
Deprecated.
|
static void |
writeObjectToFileOrDie(java.lang.Object obj,
java.lang.String file)
Serializes an object to a file, throws an exception if it fails
|
static void |
writeObjectToFileOrDie(java.lang.Object obj,
java.lang.String file,
org.apache.log4j.Logger log)
Serializes an object to a file, throws an exception if it fails
|
static void |
writeToTextFile(java.lang.String[] lines,
java.lang.String file)
Deprecated.
|
static void |
writeToTextFileOrDie(java.lang.String[] lines,
java.lang.String file) |
public static java.lang.String buildPath(java.lang.String... parts)
public static void writeObjectToFileOrDie(@Nonnull
java.lang.Object obj,
@Nonnull
java.lang.String file)
throws java.io.IOException
obj - object to write to a filefile - path to save the object tojava.io.IOException - if the existing file could not be erased, or the file could not be written, flushed, synced, or closedpublic static void writeObjectToFileOrDie(@Nonnull
java.lang.Object obj,
@Nonnull
java.lang.String file,
@Nonnull
org.apache.log4j.Logger log)
throws java.io.IOException
obj - object to write to a filefile - path to save the object tolog - logger to usejava.io.IOException - if the existing file could not be erased, or the file could not be written, flushed, synced, or closedpublic static boolean writeObjectIfChangedOrDie(@Nonnull
java.lang.Object obj,
@Nonnull
java.lang.String file)
throws java.io.IOException
obj - object to write to a filefile - path to save the object tojava.io.IOException - if the existing file could not be read for comparison,
if the existing file could not be erased,
or if the new file could not be written, flushed, synced, or closedpublic static boolean writeObjectIfChangedOrDie(@Nonnull
java.lang.Object obj,
@Nonnull
java.lang.String file,
@Nonnull
org.apache.log4j.Logger log)
throws java.io.IOException
obj - object to write to a filefile - path to save the object tolog - logger to usejava.io.IOException - if the existing file could not be read for comparison,
if the existing file could not be erased,
or if the new file could not be written, flushed, synced, or closedpublic static long computeFileChecksum(@Nonnull
java.io.File file,
@Nonnull
java.util.zip.Checksum checksum)
throws java.io.IOException
java.io.IOException@Deprecated
public static boolean writeObjectToFile(java.lang.Object obj,
java.lang.String file)
writeObjectToFileOrDie(Object, String, org.apache.log4j.Logger) instead@Deprecated
public static boolean writeObjectIfChanged(java.lang.Object obj,
java.lang.String filepath)
writeObjectIfChangedOrDie(Object, String, org.apache.log4j.Logger) insteadpublic static <T> T readObjectFromFile(java.lang.String file,
java.lang.Class<T> clazz,
boolean printException)
T from file.T - the return typefile - file from which the object should be readclazz - non-null Class object for TprintException - whether or not any stacktraces should be printedT.public static java.lang.Object readObjectFromClasspathDir(java.lang.String file)
readObjectFromFile(String file, Class clazz, boolean printException)
where:
clazz is Object.classprintException is falsepublic static <T> T readObjectFromClasspathDir(java.lang.String file,
java.lang.Class<T> clazz,
boolean printException)
public static <T> T readObjectFromFile(java.lang.String file,
java.lang.Class<T> clazz)
readObjectFromFile(String file, Class clazz, boolean printException)
where:
printException is falsepublic static java.lang.Object readObjectFromFile(java.lang.String file,
boolean printException)
readObjectFromFile(String file, Class clazz, boolean printException)
where:
clazz is Object.classpublic static java.lang.Object readObjectFromFile(java.lang.String file)
readObjectFromFile(String file, Class clazz, boolean printException)
where:
clazz is Object.classprintException is falsepublic static java.lang.String[] readTextFile(java.lang.String file)
public static java.lang.String[] readTextFileOrDie(java.lang.String file)
throws java.io.IOException
java.io.IOExceptionpublic static java.util.List<java.lang.String> readCommentedTextFile(java.lang.String file)
throws java.io.IOException
file - java.io.IOExceptionpublic static int[] readIntsFromTextFile(java.lang.String file)
public static float[] readFloatsFromTextFile(java.lang.String file)
@Deprecated
public static void writeToTextFile(java.lang.String[] lines,
java.lang.String file)
public static void writeToTextFileOrDie(@Nonnull
java.lang.String[] lines,
@Nonnull
java.lang.String file)
throws java.io.IOException
java.io.IOExceptionpublic static void appendToTextFile(java.lang.String[] lines,
java.lang.String file)
public static java.lang.String getTempDirectory(java.lang.String prefix,
java.lang.String suffix)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String getTempDirectory(java.lang.String prefix,
java.lang.String suffix,
java.lang.String directory)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String getTempDirectory(java.lang.String prefix,
java.lang.String suffix,
java.io.File directory)
throws java.io.IOException
java.io.IOExceptionpublic static boolean delete(java.lang.String file)
deleteOrDie(String) instead if you want a clearer distinction between the 'falsy' responsesfile - path to erasepublic static boolean deleteOrDie(@Nonnull
java.lang.String file)
throws java.io.IOException
file - path to erasejava.io.IOException - if deletion fails and the file still exists at the endpublic static java.lang.String getCanonicalPath(java.lang.String path)
File.getCanonicalPath(), as that method
can fail during symlink changes, and this method will retry up to 3 times with a
short delay. Returns null if unsuccessful after retrying.path - path to canonicalizepublic static java.lang.String getCanonicalDirectoryName(java.lang.String path)
getCanonicalPath(String).path - path to canonicalizepublic static byte[] loadFileAsByteArray(java.lang.String file)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String getFileHash(java.lang.String file,
java.lang.String algorithm)
throws java.io.IOException,
java.security.NoSuchAlgorithmException
java.io.IOExceptionjava.security.NoSuchAlgorithmException@Nonnull
public static java.lang.String toHex(@Nonnull
byte[] bytes)
bytes.length * 2.public static java.io.BufferedReader getBufferedReaderForUtf8(java.lang.String file)
throws java.io.FileNotFoundException
java.io.FileNotFoundExceptionpublic static java.lang.String readStreamAsString(java.io.InputStream in)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String readStreamAsString(java.io.InputStream in,
int bufferSize)
throws java.io.IOException
java.io.IOExceptionpublic static java.io.Writer newBufferedUTF8FileWriter(java.lang.String file)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
FileWriter because you cannot specify the character encoding with that.java.io.UnsupportedEncodingExceptionjava.io.FileNotFoundExceptionpublic static java.io.Writer newBufferedUTF8FileWriter(java.io.File file)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
FileWriter because you cannot specify the character encoding with that.java.io.UnsupportedEncodingExceptionjava.io.FileNotFoundException