Class FileUtils


  • public class FileUtils
    extends java.lang.Object
    A utility class for File creation and manipulation.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FileUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void appendLines​(java.io.File file, java.util.Collection<java.lang.String> lines)  
      static void copy​(java.lang.String src, java.lang.String dst)  
      static java.lang.String createTempFileWithLines​(java.lang.String suffix, java.lang.Iterable<java.lang.String> lines)
      Create a temporary file (see File.createTempFile(String, String)) and fill it with the given lines.
      static java.io.File extractFromResource​(java.lang.String name)
      Helper method for extracting a given resource to File
      static java.util.Collection<java.io.File> getClasses​(java.io.File root)  
      static void purgeTemps()  
      static java.util.Collection<java.lang.String> readAllLines​(java.io.File file)  
      static java.util.Collection<java.lang.String> readAllLines​(java.io.InputStream stream)  
      static java.util.Collection<java.lang.String> readAllLines​(java.io.Reader src)  
      static <T extends java.io.Flushable & java.io.Closeable>
      void
      safelyClose​(T obj)  
      static java.util.Collection<java.lang.String> tail​(java.io.File file, int num)  
      static java.io.File tempFile​(java.lang.String suffix)
      Creates the temp file with given suffix.
      static void touch​(java.lang.String f)  
      static TempFile weakTempFile​(java.lang.String suffix)
      Creates the temp file, and retains it as long as the reference to it is reachable.
      static void writeLines​(java.io.File file, java.util.Collection<java.lang.String> lines)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • FileUtils

        private FileUtils()
    • Method Detail

      • weakTempFile

        public static TempFile weakTempFile​(java.lang.String suffix)
                                     throws java.io.IOException
        Creates the temp file, and retains it as long as the reference to it is reachable.
        Parameters:
        suffix - suffix
        Returns:
        temp file
        Throws:
        java.io.IOException - if things go crazy
      • purgeTemps

        public static void purgeTemps()
      • tempFile

        public static java.io.File tempFile​(java.lang.String suffix)
                                     throws java.io.IOException
        Creates the temp file with given suffix. The file would be removed on JVM exit, or when caller deletes the file itself.
        Parameters:
        suffix - suffix
        Returns:
        temporary file
        Throws:
        java.io.IOException - if things go crazy
      • extractFromResource

        public static java.io.File extractFromResource​(java.lang.String name)
                                                throws java.io.IOException
        Helper method for extracting a given resource to File
        Parameters:
        name - name of the resource
        Returns:
        a File pointing to the extracted resource
        Throws:
        java.io.IOException - if things go crazy
      • createTempFileWithLines

        public static java.lang.String createTempFileWithLines​(java.lang.String suffix,
                                                               java.lang.Iterable<java.lang.String> lines)
                                                        throws java.io.IOException
        Create a temporary file (see File.createTempFile(String, String)) and fill it with the given lines.
        Parameters:
        suffix - file suffix File.createTempFile(String, String)
        lines - to be written
        Returns:
        the temporary file absolute path
        Throws:
        java.io.IOException - on file creation error
      • tail

        public static java.util.Collection<java.lang.String> tail​(java.io.File file,
                                                                  int num)
                                                           throws java.io.IOException
        Throws:
        java.io.IOException
      • readAllLines

        public static java.util.Collection<java.lang.String> readAllLines​(java.io.Reader src)
                                                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • readAllLines

        public static java.util.Collection<java.lang.String> readAllLines​(java.io.File file)
                                                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • readAllLines

        public static java.util.Collection<java.lang.String> readAllLines​(java.io.InputStream stream)
                                                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • writeLines

        public static void writeLines​(java.io.File file,
                                      java.util.Collection<java.lang.String> lines)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • appendLines

        public static void appendLines​(java.io.File file,
                                       java.util.Collection<java.lang.String> lines)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • getClasses

        public static java.util.Collection<java.io.File> getClasses​(java.io.File root)
      • copy

        public static void copy​(java.lang.String src,
                                java.lang.String dst)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • safelyClose

        public static <T extends java.io.Flushable & java.io.Closeable> void safelyClose​(T obj)
      • safelyClose

        public static <T extends java.io.Closeable> void safelyClose​(T obj)
      • touch

        public static void touch​(java.lang.String f)
                          throws java.io.IOException
        Throws:
        java.io.IOException