Results 1 to 3 of 3
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Rep Power

    Default Is there a relationship between I/O streams and Reader and Writer classes?

    I have read that all byte stream classes descend from InputStream and OutputStream; and all character stream classes descend from Reader and Writer.

    I expected that Reader and Writer (represent streams of characters) would be derived from InputStream and OutputStream classes (represent streams of bytes). Because character also exists in memory as 2 bytes (16 bits). So a stream of characters sounds like a specialized form of a stream of bytes.

    But according to the Java docs (1, 2, 3, 4), there is no direct inheritance relation among them.

    Though InputStream and Reader have similar subclasses, like FileInputStream, FileReader, FilterInputStream, FilterReader etc. Same is case with OutputStream and Writer.

    So I want to ask that wouldn't it make more sense if character stream classes extended byte stream classes? Is there any relationship between those which I am not seeing?

    Secondly System.out is a PrintStream object right? Which is an OutputStream. But its print and println etc. methods are used to print Strings, even though Strings are sequence of characters. This all is confusing.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Is there a relationship between I/O streams and Reader and Writer classes?

    Inheritance relationships are not required to make use of the classes. InputStream is used internally by FileReader (via other classes).
    Character streams are used to provide conversion capabilities. Remember there is ASCII, and a variety of flavors of unicode, etc.

    I recommend you check out the Basic I/O section of the tutorials in my signature. It is a fairly good overview.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Ewe Loon's Avatar
    Ewe Loon is offline Member
    Join Date
    Jun 2016
    Rep Power

    Default Re: Is there a relationship between I/O streams and Reader and Writer classes?

    I just scanned 2 classes , File Reader, and FileInputStream other than both of them both being members of Object (as all java classes are) they both implement Closeable
    from my experience I have found Readers and Writers are designed for reading and writing to Text based documents and Streams are Designed to read and write Binary Documents
    If you need to read and write to files containing both Binary and Text you might want to look a a class called RandomAccessFile

Similar Threads

  1. Replies: 2
    Last Post: 04-22-2016, 07:38 PM
  2. Replies: 1
    Last Post: 06-13-2014, 07:10 AM
  3. relationship between classes
    By talia in forum New To Java
    Replies: 1
    Last Post: 12-10-2011, 03:33 PM
  4. [SOLVED] reader and writer on same file handle
    By Nicholas Jordan in forum Advanced Java
    Replies: 11
    Last Post: 07-01-2008, 03:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts