File I/O Overview

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. public abstract int read()
  2.                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. public int available()
  2.               throws IOException
public int available()
              throws IOException


close()

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

  1. public void close()
  2.            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. public abstract void write(int b)
  2.                     throws IOException
public abstract void write(int b)
                    throws IOException


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

  1. public void write(byte[] b)
  2.            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. public void flush()
  2.            throws IOException
public void flush()
           throws IOException


close()

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

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

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on