Class File

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<java.io.File>, IModifiable
    Direct Known Subclasses:
    Folder

    public class File
    extends java.io.File
    implements IModifiable
    Simple extension of File that adds an implementation of IModifiable for files. This allows the ModificationWatcher class to watch files for modification. The IModifiable.lastModifiedTime() method also returns a Time object with a more convenient API than either Date or a value in milliseconds.
    Author:
    Jonathan Locke
    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class java.io.File

        pathSeparator, pathSeparatorChar, separator, separatorChar
    • Constructor Summary

      Constructors 
      Constructor Description
      File​(java.io.File file)
      Construct.
      File​(java.io.File parent, java.lang.String child)
      Construct.
      File​(java.lang.String pathname)
      Constructor.
      File​(java.lang.String parent, java.lang.String child)
      Constructor.
      File​(java.net.URI uri)
      Constructor.
      File​(File parent, java.lang.String child)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      File file​(java.lang.String name)  
      java.lang.String getExtension()  
      Folder getParentFolder()  
      java.io.InputStream inputStream()  
      java.time.Instant lastModifiedTime()
      Returns a Time object representing the most recent time this file was modified.
      java.io.OutputStream outputStream()
      Creates a buffered output stream that writes to this file.
      java.lang.Object readObject()  
      java.lang.String readString()  
      boolean remove()  
      void sync()
      Force contents of file to physical storage
      java.lang.String toQuotedString()  
      int write​(java.io.InputStream input)
      Writes the given input stream to this file
      void write​(java.lang.String string)
      Write the given string to this file
      int write​(File file)
      Writes the given file to this one
      void writeObject​(java.io.Serializable object)  
      • Methods inherited from class java.io.File

        canExecute, canRead, canWrite, compareTo, createNewFile, createTempFile, createTempFile, delete, deleteOnExit, equals, exists, getAbsoluteFile, getAbsolutePath, getCanonicalFile, getCanonicalPath, getFreeSpace, getName, getParent, getParentFile, getPath, getTotalSpace, getUsableSpace, hashCode, isAbsolute, isDirectory, isFile, isHidden, lastModified, length, list, list, listFiles, listFiles, listFiles, listRoots, mkdir, mkdirs, renameTo, setExecutable, setExecutable, setLastModified, setReadable, setReadable, setReadOnly, setWritable, setWritable, toPath, toString, toURI, toURL
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • File

        public File​(File parent,
                    java.lang.String child)
        Constructor.
        Parameters:
        parent - parent
        child - child
      • File

        public File​(java.io.File parent,
                    java.lang.String child)
        Construct.
        Parameters:
        parent - parent
        child - child
      • File

        public File​(java.io.File file)
        Construct.
        Parameters:
        file - File from java.io package
      • File

        public File​(java.lang.String pathname)
        Constructor.
        Parameters:
        pathname - path name
      • File

        public File​(java.lang.String parent,
                    java.lang.String child)
        Constructor.
        Parameters:
        parent - parent
        child - child
      • File

        public File​(java.net.URI uri)
        Constructor.
        Parameters:
        uri - file uri
    • Method Detail

      • file

        public File file​(java.lang.String name)
        Parameters:
        name - Name of child file
        Returns:
        Child file object
      • getExtension

        public java.lang.String getExtension()
        Returns:
        File extension (whatever is after the last '.' in the file name)
      • getParentFolder

        public Folder getParentFolder()
        Returns:
        Parent folder
      • inputStream

        public java.io.InputStream inputStream()
                                        throws java.io.FileNotFoundException
        Returns:
        Input stream that reads this file
        Throws:
        java.io.FileNotFoundException - Thrown if the file cannot be found
      • lastModifiedTime

        public java.time.Instant lastModifiedTime()
        Returns a Time object representing the most recent time this file was modified.
        Specified by:
        lastModifiedTime in interface IModifiable
        Returns:
        This file's lastModified() value as a Time object or null if that information is not available
      • outputStream

        public java.io.OutputStream outputStream()
                                          throws java.io.FileNotFoundException
        Creates a buffered output stream that writes to this file. If the parent folder does not yet exist, creates all necessary folders in the path.
        Returns:
        Output stream that writes to this file
        Throws:
        java.io.FileNotFoundException - Thrown if the file cannot be found
      • readString

        public java.lang.String readString()
                                    throws java.io.IOException
        Returns:
        String read from this file
        Throws:
        java.io.IOException
      • readObject

        public java.lang.Object readObject()
                                    throws java.io.IOException,
                                           java.lang.ClassNotFoundException
        Returns:
        Object read from serialization file
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeObject

        public void writeObject​(java.io.Serializable object)
                         throws java.io.IOException
        Parameters:
        object - Object to write to this file
        Throws:
        java.io.IOException
      • remove

        public boolean remove()
        Returns:
        True if the file was removed
        See Also:
        File.delete()
      • sync

        public void sync()
                  throws java.io.IOException
        Force contents of file to physical storage
        Throws:
        java.io.IOException
      • toQuotedString

        public java.lang.String toQuotedString()
        Returns:
        This file in double quotes (useful for passing to commands and tools that have issues with spaces in filenames)
      • write

        public int write​(File file)
                  throws java.io.IOException
        Writes the given file to this one
        Parameters:
        file - The file to copy
        Returns:
        number of bytes written
        Throws:
        java.io.IOException
      • write

        public int write​(java.io.InputStream input)
                  throws java.io.IOException
        Writes the given input stream to this file
        Parameters:
        input - The input
        Returns:
        Number of bytes written
        Throws:
        java.io.IOException
      • write

        public void write​(java.lang.String string)
                   throws java.io.IOException
        Write the given string to this file
        Parameters:
        string - The string to write
        Throws:
        java.io.IOException