Java IO | Basic Input-output in Java with Examples

Here, we have discussed about the Java Input and output. Java I/O is the package in Java programming language which helps in input and output....

Welcome to another interesting article on Java programming language

Java IO | Basic Input-output in Java with Examples

In this article we will going to learn about Java Input and output.

Java I/O

Java I/O is the package in Java programming language which helps in input and output operations. In Java programming language, the concept of stream is used in order to make the input and output operations faster. In Java, stream is a sequence. 

In order to execute I/O operation in Java, these streams supports all types of data types, files, objects, characters, etc. There are three types of streams in Java that are created automatically which are discussed as follows;

Java IO | Basic Input-output in Java with Examples

1. System.in

is the standard input stream which used by a programmer to read the character from any input device such as Keyboard.

2. System.out

is the standard output stream which is used to get result of a particular program on a output device such as the screen of the computer. In this output stream three types of functions are used to get the output. These functions are as follows;

print()

function is used to show the text on the console in Java programming language. By using this function, the cursor remains at the end of the text after printing it on the console. And from there, the next printing takes place.

Example;

class Output {

    public static void main(String[] args) {

        System.out.print("1. Java ");

        System.out.print("2. Input and Output");

    }

}

Output

1. Java 2. Input and Output

printIn()

 function is also used to show the text on the console in Java but by using this function the cursor moves to the next line of the console after printing the text. And from the new line, the next printing takes place.

Example;

class Output {

    public static void main(String[] args) {

        System.out.println("1. Mango ");

        System.out.println("2. Banana ");

    }

}

Output

1. Mango

2. Banana

printf()

function is the easiest among all and it can be used by the programmers to format the output in Java programming language. printf() functions can deals with the multiple arguments. 

Example;

class Output {

    public static void main(String[] args) {

        System.out.printf("Learn ");

        System.out.printf("Java");

    }

}

Output

Learn Java

3. System.err

is the standard error stream which is used by the programmer to show the error data as output in the output device such as the screen of the computer. In this error stream there also three types of functions are used to get the output i.e.,

a. print()

b. printf()

c. printIn()

Let's understand with help of following example;

import java.io.*; 

public class SimpleIO { 

    public static void main(String args[]) 


        throws IOException 

    { 

        // InputStreamReader class to read input 

        InputStreamReader inp = null; 

        // Storing the input in inp 

        inp = new InputStreamReader(System.in); 

        System.out.println("Enter characters, "

                           + " and '0' to quit."); 

        char c; 

        do { 

   

            c = (char)inp.read(); 

            System.out.println(c); 

        } while (c != '0'); 

    } 

}

Input

Programming

Output

Enter characters, and '0' to quit.

P

r

o

g

r

a

m

m

i

n

g

0

Conclusion

Above we have discussed about the Java Input and output. Java I/O is the package in Java programming language which helps in input and output operations. In Java programming language, the concept of stream is used in order to make the input and output operations faster. We have learnt that there are three types of streams in Java programming language which are created automatically.