Class Files


  • public class Files
    extends java.lang.Object
    Utility operations on files.
    Author:
    Abe White
    • Constructor Summary

      Constructors 
      Constructor Description
      Files()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.File backup​(java.io.File file, boolean copy)
      Backup the given file to a new file called <file-name>~.
      static boolean copy​(java.io.File from, java.io.File to)
      Copy a file.
      static java.io.File getClassFile​(java.lang.Class cls)
      Return the class file of the given class, or null if the class is in a jar.
      static java.io.File getFile​(java.lang.String name, java.lang.ClassLoader loader)
      Check the given string for a matching file.
      static java.io.OutputStream getOutputStream​(java.lang.String file, java.lang.ClassLoader loader)
      Return an output stream to the stream(stdout or stderr) or file named by the given string.
      static java.io.File getPackageFile​(java.io.File base, java.lang.String pkg, boolean mkdirs)
      Return the file for the given package.
      static java.io.File getSourceFile​(java.lang.Class cls)
      Return the source file for the given class, or null if the source is not in the CLASSPATH.
      static java.io.Writer getWriter​(java.lang.String file, java.lang.ClassLoader loader)
      Return a writer to the stream(stdout or stderr) or file named by the given string.
      static java.io.Writer getWriter​(java.lang.String file, java.lang.ClassLoader loader, java.lang.String enc)
      Return a writer to the stream(stdout or stderr) or file named by the given string set to the provided charset encoding.
      static java.io.File revert​(java.io.File backup, boolean copy)
      Revert the given backup file to the original location.
      • Methods inherited from class java.lang.Object

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

      • Files

        public Files()
    • Method Detail

      • backup

        public static java.io.File backup​(java.io.File file,
                                          boolean copy)
        Backup the given file to a new file called <file-name>~. If the file does not exist or a backup could not be created, returns null.
      • revert

        public static java.io.File revert​(java.io.File backup,
                                          boolean copy)
        Revert the given backup file to the original location. If the given file's name does not end in '~', the '~' is appended before proceeding. If the backup file does not exist or could not be reverted, returns null.
      • getSourceFile

        public static java.io.File getSourceFile​(java.lang.Class cls)
        Return the source file for the given class, or null if the source is not in the CLASSPATH.
      • getClassFile

        public static java.io.File getClassFile​(java.lang.Class cls)
        Return the class file of the given class, or null if the class is in a jar.
      • getPackageFile

        public static java.io.File getPackageFile​(java.io.File base,
                                                  java.lang.String pkg,
                                                  boolean mkdirs)
        Return the file for the given package. If the given base directory matches the given package structure, it will be used as-is. If not, the package structure will be added beneath the base directory. If the base directory is null, the current working directory will be used as the base.
      • getFile

        public static java.io.File getFile​(java.lang.String name,
                                           java.lang.ClassLoader loader)
        Check the given string for a matching file. The string is first tested to see if it is an existing file path. If it does not represent an existing file, it is checked as a resource name of a file. If no resource exists, then it is interpreted as a path to a file that does not exist yet.
        Parameters:
        name - the file path or resource name
        loader - a class loader to use in resource lookup, or null to use the thread's context loader
      • getWriter

        public static java.io.Writer getWriter​(java.lang.String file,
                                               java.lang.ClassLoader loader)
                                        throws java.io.IOException
        Return a writer to the stream(stdout or stderr) or file named by the given string.
        Throws:
        java.io.IOException
        See Also:
        getFile(java.lang.String, java.lang.ClassLoader)
      • getWriter

        public static java.io.Writer getWriter​(java.lang.String file,
                                               java.lang.ClassLoader loader,
                                               java.lang.String enc)
                                        throws java.io.IOException
        Return a writer to the stream(stdout or stderr) or file named by the given string set to the provided charset encoding.
        Throws:
        java.io.IOException
        See Also:
        getOutputStream(java.lang.String, java.lang.ClassLoader)
      • getOutputStream

        public static java.io.OutputStream getOutputStream​(java.lang.String file,
                                                           java.lang.ClassLoader loader)
        Return an output stream to the stream(stdout or stderr) or file named by the given string.
        See Also:
        getFile(java.lang.String, java.lang.ClassLoader)
      • copy

        public static boolean copy​(java.io.File from,
                                   java.io.File to)
                            throws java.io.IOException
        Copy a file. Return false if from does not exist.
        Throws:
        java.io.IOException