File Reader and File Writer class with its methods and examples

FileReader class:
  • Syntax: 
          java.lang.Object
                  java.io.Reader
                          java.io.InputStreamReader
                                  java.io.FileReader
  • It is most convince class for writing character files. 
  • The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. 
  • It has one field, three constructors and seventeen methods.
    • The four methods derived from the InputStreamReader class.
    • The five methods derived from the Reader class.
    • The eight methods derived from the Object class.
  • Field: protected Object lock
    • Use: The object used to synchronize operations on this stream
    • This field is inherited from class java.io.Reader
  • Constructor: 
    • Syntax:
      • public FileReader(File file) throws FileNotFoundException
      • public FileReader(FileDescriptor fd)
      • public FileReader(String fileName) throws FileNotFoundException
    • Use:
      • Creates a new FileReader, given the File to read from.
      • Creates a new FileReader, given the FileDescriptor to read from.
    • Parameters:
      • file - the File to read from
      • fd - the FileDescriptor to read from
      • fileName - the name of the file to read from
    • Throws:
      • FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
  • Methods:
    • The following methods are inherited from class java.io.InputStreamReader
      • close, getEncoding, read, ready
    • The following methods are inherited from class java.io.Reader
      • mark, markSupported, read, reset, skip
    • The following methods are inherited from class java.lang.Object
      • clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait
FileWriter class:
  • Syntax: 
          java.lang.Object
                  java.io.Reader
                          java.io.OutputStreamReader

                                  java.io.FileWriter
  • Convenience class for writing character files. 
  • The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. 
  • FileWriter is meant for writing streams of characters. 
  • For writing streams of raw bytes, consider using a FileOutputStream.
  • It has one field, six constructors and fifteen methods.
    • The four methods derived from the OutputStreamReader class.
    • The two methods derived from the Writer class.
    • The nine methods derived from the Object class.
  • Field: protected Object lock
    • Use: The object used to synchronize operations on this stream
    • This field is inherited from class java.io.Writer
  • Constructor: 
    • Syntax:
      • public FileWriter(File file) throws IOException
      • public FileWriter(File file, boolean append) throws IOException
      • public FileWriter(FileDescriptor fd)
      • public FileWriter(String fileName) throws IOException
      • public FileWriter(String fileName, boolean append) throws IOException
    • Use:
      • Constructs a FileWriter object given a File object.
      • Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
      • Constructs a FileWriter object associated with a file descriptor.
      • Constructs a FileWriter object given a file name.
      • Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
    • Parameters:
      • file - a File object to write to.
      • append - if true, then bytes will be written to the end of the file rather than the beginning
      • fd - FileDescriptor object to write to.
      • fileName - String The system-dependent filename.
    • Throws:
      • IOException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
  • Methods:
    • The following methods are inherited from class java.io.OutputStreamWriter
      • close, flush, getEncoding, write
    • The following methods are inherited from class java.io.Writer
      • append, write
    • The following methods are inherited from class java.lang.Object
      • clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait
Example (Read File):
  • Step 01: Create a .txt file.
  • Step 02: Write a JAVA program.
  • Step 03: Enjoy the Output.
Example (Write File):
  • Step 01: Write a JAVA program.
  • Step 02: Enjoy the Output.
  • Step 03: Read data from File, also.
Example (Copy File):
  • Step 01:Write a .txt file. 
  • Step 02: Write a .JAVA program.
  • Step 03: Run the program
  • Step 04: See the output in file, also.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form