Class Files


  • public class Files
    extends java.lang.Object
    • 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.IOException
        Serializes an object to a file, throws an exception if it fails
        Parameters:
        obj - object to write to a file
        file - 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.IOException
        Throws:
        java.io.IOException
      • writeObjectIfChangedOrDie2

        public static boolean writeObjectIfChangedOrDie2​(@Nonnull
                                                         java.lang.Object obj,
                                                         @Nonnull
                                                         java.lang.String file)
                                                  throws java.io.IOException
        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. Note that you must have enough heap to contain the entire contents of the object graph.
        Parameters:
        obj - object to write to a file
        file - 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.IOException
        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)
        Writes 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)
        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. 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 type T from file.
        Type Parameters:
        T - the return type
        Parameters:
        file - file from which the object should be read
        clazz - non-null Class object for T
        printException - whether or not any stacktraces should be printed
        Returns:
        possibly null object of type T.
      • readObjectFromClasspathDir

        public static <T> T readObjectFromClasspathDir​(java.lang.String file,
                                                       java.lang.Class<T> clazz,
                                                       boolean printException)
      • 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.IOException
        Reads 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 directory

        NOTE: 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.IOException
        Deletes 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 of 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.
        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 of getCanonicalPath(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.IOException
        java.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 exactly bytes.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.FileNotFoundException
        Use this instead of FileWriter because you cannot specify the character encoding with that.
        Throws:
        java.io.UnsupportedEncodingException
        java.io.FileNotFoundException
      • newBufferedUTF8FileWriter

        public static java.io.Writer newBufferedUTF8FileWriter​(java.io.File file)
                                                        throws java.io.UnsupportedEncodingException,
                                                               java.io.FileNotFoundException
        Use this instead of FileWriter because you cannot specify the character encoding with that.
        Throws:
        java.io.UnsupportedEncodingException
        java.io.FileNotFoundException