Class FileWrapper

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class FileWrapper
    extends java.lang.Object
    implements java.io.Closeable
    File wrapper for text files. Makes it really easy to open, close, delete, read, and write text files.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.io.File file  
      private java.io.FileInputStream fis  
      private java.io.FileOutputStream fos  
      private java.io.InputStreamReader isr  
      private java.io.OutputStreamWriter osw  
      private java.io.BufferedReader reader  
      private FileWrapper.FileState state  
      private java.io.BufferedWriter writer  
    • Constructor Summary

      Constructors 
      Constructor Description
      FileWrapper​(java.io.File file)
      Create a new FileWrapper for the given File.
      FileWrapper​(java.io.File root, java.lang.String str)  
      FileWrapper​(java.lang.String str)  
    • Field Detail

      • file

        private final java.io.File file
      • fis

        private java.io.FileInputStream fis
      • isr

        private java.io.InputStreamReader isr
      • reader

        private java.io.BufferedReader reader
      • fos

        private java.io.FileOutputStream fos
      • osw

        private java.io.OutputStreamWriter osw
      • writer

        private java.io.BufferedWriter writer
    • Constructor Detail

      • FileWrapper

        public FileWrapper​(java.io.File file)
        Create a new FileWrapper for the given File. Represents the same file in the filesystem as the underlying File object. getBase() return the FileWrapper for the file system root.
        Parameters:
        file - File to wrap
      • FileWrapper

        public FileWrapper​(java.lang.String str)
      • FileWrapper

        public FileWrapper​(java.io.File root,
                           java.lang.String str)
    • Method Detail

      • canWrite

        public boolean canWrite()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • isYoungerThan

        boolean isYoungerThan​(FileWrapper fw)
        Returns true if either this FileWrapper does not exist, or if the lastModificationTime of this FileWrapper is earlier than that of fw.
      • delete

        public void delete()
      • getName

        public java.lang.String getName()
      • getAbsoluteName

        public java.lang.String getAbsoluteName()
      • readAll

        public byte[] readAll()
                       throws java.io.IOException
        Throws:
        java.io.IOException
      • writeAll

        public void writeAll​(byte[] data)
                      throws java.io.IOException
        Throws:
        java.io.IOException
      • readLine

        public java.lang.String readLine()
                                  throws java.io.IOException
        Read the next line from the text file. File state must be FileState OPEN_FOR_READ. Returns null when at the end of file.
        Returns:
        The String just read.
        Throws:
        java.io.IOException - for IO errors.
      • writeLine

        public void writeLine​(java.lang.String line)
                       throws java.io.IOException
        Write the line to the end of the file, including a newline. File state must be FileState OPEN_FOR_WRITE.
        Parameters:
        line - The line to write.
        Throws:
        java.io.IOException - for IO errors.
      • close

        public void close()
        Close the file, and set its state to CLOSED. This method does not throw any exceptions.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • open

        public void open​(FileWrapper.OpenMode mode)
                  throws java.io.IOException
        Open the (text) file for I/O. There are two modes:
        • READ. In this mode, the file is prepared for reading, starting from the beginning. end-of-file at the time the file is opened.
        • WRITE. In this mode, the file is prepared for writing, starting at the end of the file.
        Parameters:
        mode - READ or WRITE mode.
        Throws:
        java.io.IOException - for IO exceptions.
      • copyTo

        public void copyTo​(FileWrapper target,
                           byte[] buffer)
                    throws java.io.IOException
        Copy this file to target using buffer to hold data. Does not assume we are using text files.
        Parameters:
        target - The FileWrapper to copy data to.
        buffer - The buffer to use for copying the files.
        Throws:
        java.io.IOException - for IO exceptions.