Character streams with its types

Character Streams:
  • Byte streams are used to perform input and output of 8-bit bytes
  • Character streams are used to perform input and output for 16-bit Unicode. 
  • All byte stream classes are descended from Reader and Writer
  • Figure:
  • There are many Character stream classes. 
  • When Not to Use Byte Streams?
    • When .txt contains character data, the best approach is to use character streams.
    • There are also streams for more complicated data types. 
    • Byte streams should only be used for the most primitive I/O.
  • Why talk about byte streams? 
    • Because all other stream types are built on byte streams.
    • Character streams are often "wrappers" for byte streams. 
    • The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes. 
    • For Example: 
      • FileReader uses FileInputStream, while FileWriter uses FileOutputStream.
      • There are two general-purpose byte-to-character "bridge" streams: InputStreamReader and OutputStreamWriter. 
  • Example: Classes of Reader and Writer stream: 
Reader Class:
  • Syntax: 
    java.lang.Object
              java.io.Reader
  • Abstract class for reading character streams. 
  • The only methods that a subclass must implement are read(char[], int, int) and close(). 
  • It has one field, two constructors and seven methods.
  • Field:
    • protected Object lock
  • Constructor: 
    • protected Reader()
    • protected Reader(Object lock)
  • Methods:
    • 1. close
      • Syntax: public abstract void close() throws IOException
      • Use: Closes the stream and releases any system resources associated with it. 
      • Throws: IOException - If an I/O error occurs
    • 2. mark
      • Syntax: public void mark(int readAheadLimit) throws IOException
      • Use: Marks the present position in the stream. Not all character-input streams support the mark() operation.
      • Parameters: readAheadLimit - Limit on the number of characters that may be read while still preserving the mark.
      • Throws: IOException - If the stream does not support mark(), or if some other I/O error occurs
    • 3. markSupported
      • Syntax: public boolean markSupported()
      • Use: Tells whether this stream supports the mark() operation. The default implementation always returns false. Subclasses should override this method.
      • Returns: true if and only if this stream supports the mark operation.
    • 4. read
      • Syntax: 
        • public int read() throws IOException
        • public int read(char[] cbuf) throws IOException
        • public abstract int read(char[] cbuf, int off, int len) throws IOException
        • public int read(CharBuffer target) throws IOException
      • Use: 
        • Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.
        • Subclasses that intend to support efficient single-character input should override this method.
        • Reads characters into an array. 
        • Reads characters into a portion of an array.
        • Attempts to read characters into the specified character buffer. 
      • Parameters:
        • cbuf - Destination buffer
        • off - Offset at which to start storing characters
        • len - Maximum number of characters to read
        • target - the buffer to read characters into
      • Returns:
        • The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.
      • Throws:
        • IOException - if an I/O error occurs
        • NullPointerException - if target is null
        • ReadOnlyBufferException - if target is a read-only buffer
    • 5. ready
      • Syntax: public boolean ready() throws IOException
      • Use: Tells whether this stream is ready to be read.
      • Returns: True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
      • Throws: IOException - If an I/O error occurs
    • 6. reset
      • Syntax:  public void reset() throws IOException
      • Use:  Resets the stream. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().
      • Throws: IOException - If the stream has not been marked, or if the mark has been invalidated, or if the stream does not support reset(), or if some other I/O error occurs
    • 7. skip
      • Syntax: public long skip(long n) throws IOException
      • Use: Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.
      • Parameters: n - The number of characters to skip
      • Returns: The number of characters actually skipped
      • Throws: 
        • IllegalArgumentException - If n is negative.
        • IOException - If an I/O error occurs
Writer Class:
  • Syntax: 
    java.lang.Object
              java.io.Writer
  • Abstract class for writing to character streams. 
  • The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). 
  • It has one field, two constructors and four methods.
  • Field:
    • protected Object lock
  • Constructor: 
    • protected Writer()
    • protected Writer(Object lock)
  • Methods:
    • 1. append
      • Syntax:
        • public Writer append(char c) throws IOException
        • public Writer append(CharSequence csq) throws IOException
        • public Writer append(CharSequence csq, int start, int end) throws IOException
      • Use:
        • Appends the specified character to this writer.
        • An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation: out.write(c) 

        • Appends the specified character sequence to this writer.
        • An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation: out.write(csq.toString()) 

        • Appends a subsequence of the specified character sequence to this writer. 
        • An invocation of this method of the form out.append(csq, start, end) when csq is not null behaves in exactly the same way as the invocation: out.write(csq.subSequence(start, end).toString()) 
      • Parameters:
        • c - The 16-bit character to append
        • csq - The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
        • start - The index of the first character in the subsequence
        • end - The index of the character following the last character in the subsequence
      • Returns:
        • This writer
      • Throws:
        • IOException - If an I/O error occurs
        • IndexOutOfBoundsException - If start or end are negative, start is greater than end, or end is greater than csq.length()
    • 2. close
      • Syntax: public abstract void close() throws IOException
      • Use: Closes the stream, flushing it first. 
      • Throws: IOException - If an I/O error occurs
    • 3. flush
      • Syntax: public abstract void flush() throws IOException
      • Use: Flushes the stream. 
      • Throws: IOException - If an I/O error occurs
    • 4. write
      • Syntax: 
        • public void write(char[] cbuf) throws IOException
        • public abstract void write(char[] cbuf, int off, int len) throws IOException
        • public void write(int c) throws IOException
        • public void write(String str) throws IOException
        • public void write(String str, int off, int len) throws IOException
      • Use:
        • Writes an array of characters.
        • Writes a portion of an array of characters.
        • Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
        • Writes a string.
        • Writes a portion of a string.
      • Parameters:
        • cbuf - Array of characters to be written
        • off - Offset from which to start writing characters
        • len - Number of characters to write
        • c - int specifying a character to be written
        • str - String to be written
      • Throws:
        • IndexOutOfBoundsException - If off is negative, or len is negative, or off+len is negative or greater than the length of the given string
        • IOException - If an I/O error occurs.
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.

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

Previous Post Next Post

Contact Form