Package com.indeed.util.io
Class Files
- java.lang.Object
-
- com.indeed.util.io.Files
-
public class Files extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static voidappendToTextFile(java.lang.String[] lines, java.lang.String file)static java.lang.StringbuildPath(java.lang.String... parts)static longcomputeFileChecksum(java.io.File file, java.util.zip.Checksum checksum)static booleandelete(java.lang.String file)Deletes file or recursively deletes a directorystatic booleandeleteOrDie(java.lang.String file)Deletes file or recursively deletes a directorystatic java.io.BufferedReadergetBufferedReaderForUtf8(java.lang.String file)static java.lang.StringgetCanonicalDirectoryName(java.lang.String path)Gets the directory name of the canonical path -- the last element in the result ofgetCanonicalPath(String).static java.lang.StringgetCanonicalPath(java.lang.String path)Use this function instead ofFile.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.StringgetFileHash(java.lang.String file, java.lang.String algorithm)static java.lang.StringgetTempDirectory(java.lang.String prefix, java.lang.String suffix)static java.lang.StringgetTempDirectory(java.lang.String prefix, java.lang.String suffix, java.io.File directory)static java.lang.StringgetTempDirectory(java.lang.String prefix, java.lang.String suffix, java.lang.String directory)static byte[]loadFileAsByteArray(java.lang.String file)static java.io.WriternewBufferedUTF8FileWriter(java.io.File file)Use this instead ofFileWriterbecause you cannot specify the character encoding with that.static java.io.WriternewBufferedUTF8FileWriter(java.lang.String file)Use this instead ofFileWriterbecause 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.ObjectreadObjectFromClasspathDir(java.lang.String file)Convenience forreadObjectFromFile(String file, Class clazz, boolean printException)where:clazzis Object.classprintExceptionis falsestatic <T> TreadObjectFromClasspathDir(java.lang.String file, java.lang.Class<T> clazz, boolean printException)static java.lang.ObjectreadObjectFromFile(java.lang.String file)Convenience forreadObjectFromFile(String file, Class clazz, boolean printException)where:clazzis Object.classprintExceptionis falsestatic java.lang.ObjectreadObjectFromFile(java.lang.String file, boolean printException)Convenience forreadObjectFromFile(String file, Class clazz, boolean printException)where:clazzis Object.classstatic <T> TreadObjectFromFile(java.lang.String file, java.lang.Class<T> clazz)Convenience forreadObjectFromFile(String file, Class clazz, boolean printException)where:printExceptionis falsestatic <T> TreadObjectFromFile(java.lang.String file, java.lang.Class<T> clazz, boolean printException)Reads an object of typeTfromfile.static java.lang.StringreadStreamAsString(java.io.InputStream in)static java.lang.StringreadStreamAsString(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.StringtoHex(byte[] bytes)Converts a byte array to a hex string.static booleanwriteObjectIfChanged(java.lang.Object obj, java.lang.String filepath)Deprecated.usewriteObjectIfChangedOrDie2(Object, String)insteadstatic booleanwriteObjectIfChangedOrDie(java.lang.Object obj, java.lang.String file, org.apache.log4j.Logger log)Deprecated.static booleanwriteObjectIfChangedOrDie2(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 booleanwriteObjectToFile(java.lang.Object obj, java.lang.String file)Deprecated.usewriteObjectToFileOrDie2(Object, String)insteadstatic voidwriteObjectToFileOrDie(java.lang.Object obj, java.lang.String file, org.apache.log4j.Logger log)Deprecated.static voidwriteObjectToFileOrDie2(java.lang.Object obj, java.lang.String file)Serializes an object to a file, throws an exception if it failsstatic voidwriteToTextFile(java.lang.String[] lines, java.lang.String file)Deprecated.static voidwriteToTextFileOrDie(java.lang.String[] lines, java.lang.String file)
-
-
-
Method Detail
-
buildPath
public static java.lang.String buildPath(java.lang.String... parts)
-
writeObjectToFileOrDie2
public static void writeObjectToFileOrDie2(@Nonnull java.lang.Object obj, @Nonnull java.lang.String file) throws java.io.IOExceptionSerializes an object to a file, throws an exception if it fails- Parameters:
obj- object to write to a filefile- path to save the object to- Throws:
java.io.IOException- if the existing file could not be erased, or the file could not be written, flushed, synced, or closed
-
writeObjectToFileOrDie
@Deprecated public static void writeObjectToFileOrDie(@Nonnull java.lang.Object obj, @Nonnull java.lang.String file, @Nonnull org.apache.log4j.Logger log) throws java.io.IOExceptionDeprecated.- Throws:
java.io.IOException
-
writeObjectIfChangedOrDie2
public static boolean writeObjectIfChangedOrDie2(@Nonnull java.lang.Object obj, @Nonnull java.lang.String file) throws java.io.IOExceptionWrites an object to a file only if it is different from the current contents of the file, or if the file does not exist. Note that you must have enough heap to contain the entire contents of the object graph.- Parameters:
obj- object to write to a filefile- path to save the object to- Returns:
- true if the file was actually written, false if the file was unchanged
- Throws:
java.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 closed
-
writeObjectIfChangedOrDie
@Deprecated public static boolean writeObjectIfChangedOrDie(@Nonnull java.lang.Object obj, @Nonnull java.lang.String file, @Nonnull org.apache.log4j.Logger log) throws java.io.IOExceptionDeprecated.- Throws:
java.io.IOException
-
computeFileChecksum
public static long computeFileChecksum(@Nonnull java.io.File file, @Nonnull java.util.zip.Checksum checksum) throws java.io.IOException- Throws:
java.io.IOException
-
writeObjectToFile
@Deprecated public static boolean writeObjectToFile(java.lang.Object obj, java.lang.String file)Deprecated.usewriteObjectToFileOrDie2(Object, String)insteadWrites an object to a file.- Returns:
- true if the file was successfully written, false otherwise
-
writeObjectIfChanged
@Deprecated public static boolean writeObjectIfChanged(java.lang.Object obj, java.lang.String filepath)Deprecated.usewriteObjectIfChangedOrDie2(Object, String)insteadWrites an object to a file only if it is different from the current contents of the file, or if the file does not exist. Note that you must have enough heap to contain the entire contents of the object graph.- Returns:
- true if the file was actually written, false otherwise
-
readObjectFromFile
public static <T> T readObjectFromFile(java.lang.String file, java.lang.Class<T> clazz, boolean printException)Reads an object of typeTfromfile.- Type Parameters:
T- the return type- Parameters:
file- file from which the object should be readclazz- non-null Class object forTprintException- whether or not any stacktraces should be printed- Returns:
- possibly null object of type
T.
-
readObjectFromClasspathDir
public static java.lang.Object readObjectFromClasspathDir(java.lang.String file)
Convenience forreadObjectFromFile(String file, Class clazz, boolean printException)where:clazzis Object.classprintExceptionis false
-
readObjectFromClasspathDir
public static <T> T readObjectFromClasspathDir(java.lang.String file, java.lang.Class<T> clazz, boolean printException)
-
readObjectFromFile
public static <T> T readObjectFromFile(java.lang.String file, java.lang.Class<T> clazz)Convenience forreadObjectFromFile(String file, Class clazz, boolean printException)where:printExceptionis false
-
readObjectFromFile
public static java.lang.Object readObjectFromFile(java.lang.String file, boolean printException)Convenience forreadObjectFromFile(String file, Class clazz, boolean printException)where:clazzis Object.class
-
readObjectFromFile
public static java.lang.Object readObjectFromFile(java.lang.String file)
Convenience forreadObjectFromFile(String file, Class clazz, boolean printException)where:clazzis Object.classprintExceptionis false
-
readTextFile
public static java.lang.String[] readTextFile(java.lang.String file)
-
readTextFileOrDie
public static java.lang.String[] readTextFileOrDie(java.lang.String file) throws java.io.IOException- Throws:
java.io.IOException
-
readCommentedTextFile
public static java.util.List<java.lang.String> readCommentedTextFile(java.lang.String file) throws java.io.IOExceptionReads all the lines in the given file, truncating everything that happens after # (including the #)- Parameters:
file-- Returns:
- a List of the lines in the file in the order they appear (whitespace trimmed)
- Throws:
java.io.IOException
-
readIntsFromTextFile
public static int[] readIntsFromTextFile(java.lang.String file)
-
readFloatsFromTextFile
public static float[] readFloatsFromTextFile(java.lang.String file)
-
writeToTextFile
@Deprecated public static void writeToTextFile(java.lang.String[] lines, java.lang.String file)Deprecated.
-
writeToTextFileOrDie
public static void writeToTextFileOrDie(@Nonnull java.lang.String[] lines, @Nonnull java.lang.String file) throws java.io.IOException- Throws:
java.io.IOException
-
appendToTextFile
public static void appendToTextFile(java.lang.String[] lines, java.lang.String file)
-
getTempDirectory
public static java.lang.String getTempDirectory(java.lang.String prefix, java.lang.String suffix) throws java.io.IOException- Throws:
java.io.IOException
-
getTempDirectory
public static java.lang.String getTempDirectory(java.lang.String prefix, java.lang.String suffix, java.lang.String directory) throws java.io.IOException- Throws:
java.io.IOException
-
getTempDirectory
public static java.lang.String getTempDirectory(java.lang.String prefix, java.lang.String suffix, java.io.File directory) throws java.io.IOException- Throws:
java.io.IOException
-
delete
public static boolean delete(java.lang.String file)
Deletes file or recursively deletes a directoryNOTE: this returns true if the file was actually deleted, and false for 2 cases: 1. file did not exist to start with 2. File.delete() failed at some point for some reason
use
deleteOrDie(String)instead if you want a clearer distinction between the 'falsy' responses- Parameters:
file- path to erase- Returns:
- true if all deletions were successful. If a deletion fails, the method stops attempting to delete and returns false
-
deleteOrDie
public static boolean deleteOrDie(@Nonnull java.lang.String file) throws java.io.IOExceptionDeletes file or recursively deletes a directory- Parameters:
file- path to erase- Returns:
- true if all deletions were successful, false if file did not exist
- Throws:
java.io.IOException- if deletion fails and the file still exists at the end
-
getCanonicalPath
public static java.lang.String getCanonicalPath(java.lang.String path)
Use this function instead ofFile.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.- Parameters:
path- path to canonicalize- Returns:
- the canonical pathname, or null if unsuccessful
-
getCanonicalDirectoryName
public static java.lang.String getCanonicalDirectoryName(java.lang.String path)
Gets the directory name of the canonical path -- the last element in the result ofgetCanonicalPath(String).- Parameters:
path- path to canonicalize- Returns:
- canonical directory name
-
loadFileAsByteArray
public static byte[] loadFileAsByteArray(java.lang.String file) throws java.io.IOException- Throws:
java.io.IOException
-
getFileHash
public static java.lang.String getFileHash(java.lang.String file, java.lang.String algorithm) throws java.io.IOException, java.security.NoSuchAlgorithmException- Throws:
java.io.IOExceptionjava.security.NoSuchAlgorithmException
-
toHex
@Nonnull public static java.lang.String toHex(@Nonnull byte[] bytes)Converts a byte array to a hex string. The String returned will be of length exactlybytes.length * 2.
-
getBufferedReaderForUtf8
public static java.io.BufferedReader getBufferedReaderForUtf8(java.lang.String file) throws java.io.FileNotFoundException- Throws:
java.io.FileNotFoundException
-
readStreamAsString
public static java.lang.String readStreamAsString(java.io.InputStream in) throws java.io.IOException- Throws:
java.io.IOException
-
readStreamAsString
public static java.lang.String readStreamAsString(java.io.InputStream in, int bufferSize) throws java.io.IOException- Throws:
java.io.IOException
-
newBufferedUTF8FileWriter
public static java.io.Writer newBufferedUTF8FileWriter(java.lang.String file) throws java.io.UnsupportedEncodingException, java.io.FileNotFoundExceptionUse this instead ofFileWriterbecause you cannot specify the character encoding with that.- Throws:
java.io.UnsupportedEncodingExceptionjava.io.FileNotFoundException
-
newBufferedUTF8FileWriter
public static java.io.Writer newBufferedUTF8FileWriter(java.io.File file) throws java.io.UnsupportedEncodingException, java.io.FileNotFoundExceptionUse this instead ofFileWriterbecause you cannot specify the character encoding with that.- Throws:
java.io.UnsupportedEncodingExceptionjava.io.FileNotFoundException
-
-