Saturday, 22 September 2018

Characters Streams

Streams:
         Streams are used to transfer the data between program and source/destination. They transfer the data in unique way irrespective of source/destination. Streams are defined in java.io package in java. Depending up on the direction of the transfer the streams are classified in to two categories.
  • Input Stream: These streams carry the data from some source in to the program. The source may be a keyboard, a file …Here read operation is performed to input the data in to the program.
  • Output Stream: They carry the data from program to some destination. The destination may be a monitor, printer, a file … Here write operation is performed to send data from the program.
Streams that carry the data in form of characters into the program from a source or from a program to These streams carry the data in the form of chars. They use 16 bit (2 bytes) of storage to read the data. Character streams are not present form the first version of the java they are added after JDK 1.1 only. Reader is the base class for all character input streams and Writer is the base class for all character output streams.
Reader:
Reader is an abstract class and is the base class for all character input streams. Since it is abstract class objects cannot be created for Reader class. This class defines various methods which can be used in all character input streams. 
Methods:
  • int   read(): Reads next char from the stream as integer and returns -1 if no data is available in the stream.
  • int    read(char  c[]): Reads an array full of characters from the stream and returns actual number of characters read.
  • int    read(char c[], int start, int end): Reads character in to array from the specified start and end position form the stream.
  • long   available(): Returns how many number of characters yet to be read in the stream.
  • long  skip(long  n): Skips specified number of characters in the input stream and returns actual number of characters skipped
  • void  mark(int readLimit): Marks the current position and it is valid till specified read limit.
  • boolean  isMarkSupported(): Checks whether the Stream supports the mark facility or not
  • void  reset(): Moves to the recent marked position or beginning of the stream
  • void close(): Closes the stream.
Character Input Streams Hierarchy:

Stream Class Name
Use
FileReader
used to read from files
PipedReader
used to read from pipes
CharArrayReader
used to read from a char array
StringReader
used to read from a String
InputStreamReader
used to convert byte stream to character stream
BufferedReader
provides buffer facility to the Reader
PushBackReader
provides un reading facility to the Reader

Writer:
Writer is base class for all character output streams and it an abstract class. Writer defines a set of methods which are used to write the data into the streams.

Methods:
  • void    write(int c): Writes one character to output stream.
  • void     write(char c[]): Writes an array full of characters to output stream.
  • void    write(char  c[], int start, int end): Writes characters from array to output stream from the specified start and end position.
  • void  flush(): Flushes the stream i.e., immediately releases the pending data from stream.
  • void close(): Closes the output stream.
Character Output Streams Hierarchy:
Stream Class Name
Use
FileWriter
used to write data into a file
PipedWriter
used to write data to a pipe
CharArrayWriter
used to write data to a byte array
StringWriter
used to write string to a Writer
PrintWriter
used to print any data on Writer
BufferedWriter
provides buffer facility to the Writer
OutputStreamWriter
used to convert character stream to byte stream
Previous Post
Next Post

0 comments: