Solr

I/O stands for input and output

Java provides several APIs to perform input and output operations and all these APIs are bundled in a package called “java.io” package.

Input means reading data from a source

Example:
Reading data from a file
Reading data from keyboard

output  means writing data to the destination.

Example:
Writing data to a file
Writing data to the output console

We get several scenarios in real time project where we need to use these I/O operations.

We use “streams” in java to perform I/O operations.


Stream


A stream is a sequence of data.

A program uses an input stream to read data from a source, one item at a time

A program uses an output stream to write data to a destination, one item at a time

Source and destination can be anything that holds, generates or consumes data

Example:
disk files,network socket,peripheral device etc.

ip-stream

op-stream


Input stream and output stream class hierarchy


Ip-op-streams

Frequently used method of input stream


Read()

This method Reads the next byte of data from the 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.

This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

1
2
public abstract int read()
               throws IOException
public abstract int read()
               throws IOException


available()

This method returns an estimate number of bytes that can be read from the current input stream.

Returns 0 when it reaches the end of input stream.

1
2
public int available()
              throws IOException
public int available()
              throws IOException


close()

This method Closes the input steam and releases any system resources associated with the stream.

1
2
public void close()
           throws IOException
public void close()
           throws IOException


Frequently used method of output stream


write()

Writes the specified byte to this output stream.

The general contract for write is that one byte is written to the output stream.

1
2
public abstract void write(int b)
                    throws IOException
public abstract void write(int b)
                    throws IOException


Writes b.length bytes from the specified byte array to this output stream

1
2
public void write(byte[] b)
           throws IOException
public void write(byte[] b)
           throws IOException


flush()

Flushes the output stream and forces any buffered output bytes to be written out.

1
2
public void flush()
           throws IOException
public void flush()
           throws IOException


close()

Closes this output stream and releases any system resources associated with this stream.

1
2
public void close()
           throws IOException
public void close()
           throws IOException
Share this article on

Comment (9)

    1. Karibasappa G C (KB)
      Karibasappa G C (KB)

      First thing, static variables will never be a part of method, it will be a part of class always.

      So only we cant declare any static variables inside a method but we can access them inside a method.

      All static variables will be stored in separate segment inside Heap area only.

      1. The blog is very helpful. Thanks a lot for sharing! Also the “Inheritance in interface” page is not opening. Can you please fix that?

Leave a Comment

  • You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

    • Please answer this simple challenge to post your valuable comment *