Class TextFile


  • public class TextFile
    extends java.lang.Object
    A convenience class for reading text from a file.
    • Constructor Summary

      Constructors 
      Constructor Description
      TextFile​(java.io.File file)
      Instantiates a text file.
      TextFile​(java.lang.String path)
      Instantiates a text file.
      TextFile​(java.lang.String dir, java.lang.String name)
      Instantiates a text file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      TextFile append​(java.lang.String txt)
      Adds text to the current content of this file.
      TextFile appendLine​(java.lang.String txt)
      Appends text to the current content of this file and adds a new line separator at the end.
      void delete()
      Deletes file and its content from the file system.
      boolean equals​(java.lang.Object obj)  
      java.io.File getFilePath()
      Retrieves location of this text file.
      java.lang.String getPath()
      Retrieves location of this text file.
      int hashCode()  
      java.lang.String read()
      Reads content of this file.
      java.lang.String readLine​(long lnum)
      Reads specified line from this file.
      void save​(java.lang.String txt)
      Saves text as the content of this text file
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • TextFile

        public TextFile​(java.lang.String path)
        Instantiates a text file.
        Parameters:
        path - a path to the text file on the file system.
      • TextFile

        public TextFile​(java.lang.String dir,
                        java.lang.String name)
        Instantiates a text file.
        Parameters:
        dir - path to the directory, in which the file should be created.
        name - name of the file to create.
      • TextFile

        public TextFile​(java.io.File file)
        Instantiates a text file.
        Parameters:
        file - a file, in which the text is stored.
    • Method Detail

      • getPath

        public java.lang.String getPath()
        Retrieves location of this text file.
        Returns:
        path to this text file on the file system.
      • getFilePath

        public java.io.File getFilePath()
        Retrieves location of this text file.
        Returns:
        path to this text file on the file system.
      • save

        public void save​(java.lang.String txt)
                  throws java.io.IOException
        Saves text as the content of this text file
        Parameters:
        txt - a text to save in this file.
        Throws:
        java.io.IOException - when the text cannot be saved in this file.
      • append

        public TextFile append​(java.lang.String txt)
                        throws java.io.IOException
        Adds text to the current content of this file.
        Parameters:
        txt - a text to add.
        Returns:
        this text file.
        Throws:
        java.io.IOException - when saving the added text fails.
      • appendLine

        public TextFile appendLine​(java.lang.String txt)
                            throws java.io.IOException
        Appends text to the current content of this file and adds a new line separator at the end.
        Parameters:
        txt - a line of text to add to this file content
        Returns:
        this text file
        Throws:
        java.io.IOException - when saving the added line fails.
      • read

        public java.lang.String read()
                              throws java.io.IOException
        Reads content of this file.
        Returns:
        text saved in this file.
        Throws:
        java.io.IOException - if read failed.
      • readLine

        public java.lang.String readLine​(long lnum)
                                  throws java.io.IOException
        Reads specified line from this file.
        Parameters:
        lnum - number of the line to read. The line number starts from 1; there is no line 0 in a text file.
        Returns:
        text on the specified line or null, if the line number does not exist in this file.
        Throws:
        java.io.IOException - when reading the specified line fails
      • delete

        public void delete()
                    throws java.io.IOException
        Deletes file and its content from the file system.
        Throws:
        java.io.IOException - if this file cannot be deleted.
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

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