Class FileUtil


  • public class FileUtil
    extends java.lang.Object
    Simple utilities for accessing files.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copy​(java.lang.String srcFileName, java.lang.String destFileName)
      This may be a resource leak: http://bugs.sun.com/view_bug.do?bug_id=4724038 We may have to reconsider using nio for this, or apply one of the horrible workarounds listed in the bug report above.
      static java.io.FileOutputStream createFile​(java.lang.String fileName)
      Create a FileOutputStream corresponding to a particular file name.
      static void deleteContents​(java.lang.String directory)
      delete all files (recursively) in a directory.
      static java.util.Collection<java.io.File> listFiles​(java.lang.String dir, java.lang.String regex, boolean recurse)
      List all the files in a directory that match a regular expression
      static byte[] readBytes​(java.io.InputStream s)
      read fully the contents of s and return a byte array holding the result
      static void recurseFiles​(java.util.function.Consumer<java.io.File> action, java.util.function.Predicate<java.io.File> filter, java.io.File top)  
      static void writeFile​(java.io.File f, java.lang.String content)
      write string s into file f
      • Methods inherited from class java.lang.Object

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

      • FileUtil

        public FileUtil()
    • Method Detail

      • listFiles

        public static java.util.Collection<java.io.File> listFiles​(java.lang.String dir,
                                                                   java.lang.String regex,
                                                                   boolean recurse)
        List all the files in a directory that match a regular expression
        Parameters:
        recurse - recurse to subdirectories?
        Throws:
        java.lang.IllegalArgumentException - if dir is null
      • copy

        public static void copy​(java.lang.String srcFileName,
                                java.lang.String destFileName)
                         throws java.io.IOException
        This may be a resource leak: http://bugs.sun.com/view_bug.do?bug_id=4724038 We may have to reconsider using nio for this, or apply one of the horrible workarounds listed in the bug report above.
        Throws:
        java.io.IOException
      • deleteContents

        public static void deleteContents​(java.lang.String directory)
                                   throws java.io.IOException
        delete all files (recursively) in a directory. This is dangerous. Use with care.
        Throws:
        java.io.IOException - if there's a problem deleting some file
      • createFile

        public static final java.io.FileOutputStream createFile​(java.lang.String fileName)
                                                         throws java.io.IOException
        Create a FileOutputStream corresponding to a particular file name. Delete the existing file if one exists.
        Throws:
        java.io.IOException
      • readBytes

        public static byte[] readBytes​(java.io.InputStream s)
                                throws java.io.IOException
        read fully the contents of s and return a byte array holding the result
        Throws:
        java.io.IOException
      • writeFile

        public static void writeFile​(java.io.File f,
                                     java.lang.String content)
                              throws java.io.IOException
        write string s into file f
        Throws:
        java.io.IOException
      • recurseFiles

        public static void recurseFiles​(java.util.function.Consumer<java.io.File> action,
                                        java.util.function.Predicate<java.io.File> filter,
                                        java.io.File top)