Input output in java is stream based. A stream represents flow of data or sequence of bytes. Stream provides abstract view of input output.
In java two type of input and output streams are provided:-
Byte oriented – represents data in sequence of 8 bits (byte by byte read write)
Char oriented – represents data in sequence of 16 bits. (char by char read write)
Super Class of byte oriented stream:-
InputStream (read data from a source): -
This abstract class is the superclass of all classes representing an input stream of bytes.
OutputStream (write data in a stream)
This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.
Commonly used method of InputStream class:
available():
Returns the number of remaining bytes that can be read (or skipped over) from this input stream.
public synchronized int available()
read():
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
public synchronized int read()
read (byte [] b):
The total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
public int read(byte[] b) throws IOException
read(byte[] b, int off, int len):
b - the buffer into which the data is read.
off - the start offset in the destination array b
len - the maximum number of bytes read.
The total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
public synchronized int read(byte[] b, int off, int len)
public void close() throws IOException
Commonly used method of OutputStream class:
public abstract void write(int b) throws IOException
public void write(byte b[]) throws IOException
public void flush() throws IOException
public void close() throws IOException
Super Class of char oriented stream:
Reader : This Abstract class is super class for reading character streams
Commonly used methods:
public int read() throws IOException
public int read(char cbuf[]) throws IOException
abstract public void close() throws IOException
Writer : This Abstract class is super class for writing to character streams
Commonly used methods:
public void write(int c) throws IOException
public void write(char cbuf[]) throws IOException
abstract public void flush() throws IOException
abstract public void close() throws IOException;
All of the subclasses of above class ended with name of superclass like PrintWriter is subclass of Writer.
Some Important subclasses are:
PrintWriter: Prints formatted representations of objects to a text-output stream
public PrintWriter(OutputStream out)
public PrintWriter (Writer out)
public PrintWriter(OutputStream out, boolean autoFlush)
public PrintWriter(Writer out, boolean autoFlush)
public PrintWriter(String fileName) throws FileNotFoundException
public PrintWriter(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException
provides various definition of print() and println() methods.
It can print String, Object, char, long, int, float etc.
public void print(Type s)
public void println(Tpe s)
BufferedReader: Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
public BufferedReader(Reader in)
public String readLine() throws IOException
BufferedWriter
public BufferedWriter(Writer out)
Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.
A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system property line.separator.
BufferedWriter b=new BufferedWriter(new FileWriter("d:/nkbuff.txt")) ;
b.write("i am fine");
b.close();
FileInputStream: FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
public FileInputStream(String name) throws FileNotFoundException
public FileInputStream(File file) throws FileNotFoundException
FileOutputStream: A file output stream is an output stream for writing data to a file
public FileOutputStream(String name) throws FileNotFoundException
public FileOutputStream(File file) throws FileNotFoundException
public FileOutputStream(File file, boolean append) throws FileNotFoundException
public FileOutputStream(String name, boolean append) throws FileNotFoundException
FileWriter: Convenience class for writing character files
public FileWriter(String fileName) throws IOException
public FileWriter(File file) throws IOException
public FileWriter(File file, boolean append) throws IOException
public FileWriter(String fileName, boolean append) throws IOException
FileReader: Convenience class for reading character files
public FileReader(String fileName) throws FileNotFoundException
public FileReader(File file) throws FileNotFoundException
InputStreamReader
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset.
public InputStreamReader(InputStream in)
OutputStreamWriter
An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset.
public OutputStreamWriter(OutputStream out)
BufferedInputStream
public BufferedInputStream(InputStream in)
BufferedInputStream b=new BufferedInputStream(System.in);
BufferedOutputStream
BufferedOutputStream(OutputStream out)
BufferedOutputStream b=new BufferedOutputStream(new FileOutputStream("d:/nk55.txt"));
b.write(65);
b.close();
No comments:
Post a Comment