Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com
Posts

Java I/O ,Java IO : Input-output in Java with Examples

 

Java I/O 


Java I/O (Input and Output) is used to process the input and produce the output.

Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations.

We can perform file handling in Java by Java I/O API.

Stream

A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a stream because it is like a stream of water that continues to flow.

The java.io package is a built-in package in the Java programming language that provides classes for performing input and output (I/O) operations. Some of the important classes in this package are:

  1. File: A class that represents a file or directory on the file system.

  2. FileInputStream and FileOutputStream: Classes for reading and writing binary data from and to files.

  3. BufferedReader and BufferedWriter: Classes for reading and writing text data from and to files.

  4. PrintWriter: A class for writing formatted text data to a file.

  5. ObjectInputStream and ObjectOutputStream: Classes for reading and writing Java objects from and to files.

  6. InputStreamReader and OutputStreamWriter: Classes for converting byte streams to character streams and vice versa.

  7. ByteArrayInputStream and ByteArrayOutputStream: Classes for reading and writing binary data from and to a byte array.

  8. PipedInputStream and PipedOutputStream: Classes for reading and writing data between threads.

These classes and many others in the java.io package are commonly used in Java programming for a wide range of applications, including file manipulation, network communication, and serialization.'

In Java, 3 streams are created for us automatically. All these streams are attached with the console.

1) System.out: standard output stream

2) System.in: standard input stream

3) System.err: standard error stream

Let's see the code to print output and an error message to the console.

  1. System.out.println("simple message");  
  2. System.err.println("error message");  

Let's see the code to get input from console.

  1. int i=System.in.read();//returns ASCII code of 1st character  
  2. System.out.println((char)i);//will print the character  

OutputStream vs InputStream

The explanation of OutputStream and InputStream classes are given below:

OutputStream

Java application uses an output stream to write data to a destination; it may be a file, an array, peripheral device or socket.

InputStream

Java application uses an input stream to read data from a source; it may be a file, an array, peripheral device or socket.

Let's understand the working of Java OutputStream and InputStream by the figure given below.

Java IO


OutputStream class

OutputStream class is an abstract class. It is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.

Useful methods of OutputStream

MethodDescription
1) public void write(int)throws IOExceptionis used to write a byte to the current output stream.
2) public void write(byte[])throws IOExceptionis used to write an array of byte to the current output stream.
3) public void flush()throws IOExceptionflushes the current output stream.
4) public void close()throws IOExceptionis used to close the current output stream.

OutputStream Hierarchy

Java output stream hierarchy


InputStream class

InputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes.

Useful methods of InputStream

MethodDescription
1) public abstract int read()throws IOExceptionreads the next byte of data from the input stream. It returns -1 at the end of the file.
2) public int available()throws IOExceptionreturns an estimate of the number of bytes that can be read from the current input stream.
3) public void close()throws IOExceptionis used to close the current input stream.

InputStream Hierarchy

Java input stream hierarchy

Java I/O Streams

In this tutorial, we will learn about Java input/output streams and their types.

In Java, streams are the sequence of data that are read from the source and written to the destination.

An input stream is used to read data from the source. And, an output stream is used to write data to the destination.

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
  

For example, in our first Hello World example, we have used System.out to print a string. Here, the System.out is a type of output stream.

Similarly, there are input streams to take input.

Input stream reads data from source to program and output stream writes file from program to destination

We will learn about input streams and output streams in detail in the later tutorials.


Types of Streams

Depending upon the data a stream holds, it can be classified into:

  • Byte Stream
  • Character Stream

Byte Stream

Byte stream is used to read and write a single byte (8 bits) of data.

All byte stream classes are derived from base abstract classes called InputStream and OutputStream.

To learn more, visit

  • Java InputStream Class
  • Java OutputStream Class

Character Stream

Character stream is used to read and write a single character of data.

All the character stream classes are derived from base abstract classes Reader and Writer.

To learn more, visit

  • Java Reader Class
  • Java Writer Class


Post a Comment

© JAVA. The Best Codder All rights reserved. Distributed by