Java Input Output


Java Input Output

Java performs Input and Output through Streams. A Stream is the physical layer of java I/O system to perform input and output functions. Simply. File handling is done by Java I/O API.

There are two types of streams:

  1. Byte Stream : Used for handling input and output of byte. Further dived into Input Stream and Output Stream.
  2. Character Stream : Used for handling input and output of characters. It uses Unicode and therefore can be internationalized. Further divided into Reader and Writer.

In Java, the concept of input and output is essential while working on java programming. It comprises elements such as input, output, and stream. The input is the data that you provide to the program. And the output is the data that we collect from the program in the sort of a result. 

For Reading Input from Console in Java

In Java, Input can be provided either from a file or keyword. It can be read from the console in 3 methods:

  • BufferedReader
  • StringTokenizer
  • Scanner

Three streams are created by Java for us automatically. And all the streams are connected to the console.

  • 1) System.out: It is for standard output stream
  • 2) System.in: It is for standard input stream
  • 3) System.err: It is for standard error stream

OutputStream vs InputStream In Java

The description of OutputStream and InputStream classes are mentioned below:

  • The OutputStream: In Java, the application practices an output stream to compose data to a destination; it can be a file, an array, peripheral device, or socket.
  • The InputStream: In Java, the application practices an input stream to view data from a source; it can be a file, an array, peripheral device, or socket.

Example:

Com.knowledge2life import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter Website "); String name = input.nextString(); System.out.println("You entered " + name); input.close(); } }

OUTPUT:

Enter Website: (knowledge2life)
Knowledge2life