Class FileHandler


  • public final class FileHandler
    extends java.lang.Object
    Utilities for reading and writing to files
    Author:
    Bhagat
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String path
      the file path
    • Constructor Summary

      Constructors 
      Constructor Description
      FileHandler​(java.lang.String path)
      Creates a FileHandler object for one file
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void append​(java.lang.String text)
      Write to the file specified in the constructor
      static void append​(java.lang.String path, java.lang.String text)
      Appends the text to the end of the file specified by the path
      java.lang.String getPath()  
      java.lang.String read()
      Read the file specified in the constructor
      static java.lang.String read​(java.io.Reader reader)
      Reads from file specified by the reader
      static java.lang.String read​(java.lang.String path)
      Reads from file specified by the path
      void setPath​(java.lang.String path)  
      static void write​(java.io.Writer writer, java.lang.String text)
      Writes the text to the file specified by the writer
      void write​(java.lang.String text)
      Write to the file specified in the constructor
      static void write​(java.lang.String path, java.lang.String text)
      Writes the text to the file specified by the path
      • Methods inherited from class java.lang.Object

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

      • path

        private java.lang.String path
        the file path
    • Constructor Detail

      • FileHandler

        public FileHandler​(java.lang.String path)
        Creates a FileHandler object for one file
        Parameters:
        path - the file path
    • Method Detail

      • read

        public java.lang.String read()
        Read the file specified in the constructor
        Returns:
        the read text
      • write

        public void write​(java.lang.String text)
        Write to the file specified in the constructor
        Parameters:
        text - the text to be written into the file
      • append

        public void append​(java.lang.String text)
        Write to the file specified in the constructor
        Parameters:
        text - the text to be appended into the file
      • getPath

        public java.lang.String getPath()
        Returns:
        the path
      • setPath

        public void setPath​(java.lang.String path)
        Parameters:
        path - the path to set
      • read

        public static java.lang.String read​(java.lang.String path)
        Reads from file specified by the path
        Parameters:
        path - the file path
        Returns:
        the contents of the file
      • read

        public static java.lang.String read​(java.io.Reader reader)
        Reads from file specified by the reader
        Parameters:
        reader - the reader
        Returns:
        the contents of the file
      • write

        public static void write​(java.lang.String path,
                                 java.lang.String text)
        Writes the text to the file specified by the path
        Parameters:
        path - the file path
        text - the text to be written to the file
      • write

        public static void write​(java.io.Writer writer,
                                 java.lang.String text)
        Writes the text to the file specified by the writer
        Parameters:
        writer - a writer
        text - the text to be written to the file
      • append

        public static void append​(java.lang.String path,
                                  java.lang.String text)
        Appends the text to the end of the file specified by the path
        Parameters:
        path - the file path
        text - the text to be appended to the file