10. Java I/O
Why should we learn this Java I/O (input and output) concept?
Let me explain a basic example to understand the importance of IO.
Stream : A stream is a continuous flow of the data.
InputStream : A stream which receives or read the data
OutStream : A stream which sends or write the data
System.in
Let me explain a basic example to understand the importance of IO.
----------------------------------------------------------------------
Program : Java program to add two values
Program name : Addition.java
Output :
Addition of x and y is:30
class Addition
{
public static void main(String args[])
{
int x = 10;
int y = 20;
System.out.println("Addition of x and y is:"+(x+y));
}
}
Compile : javac Addition.java
Run : java Addition
Output :
Addition of x and y is:30
Run : java Addition
Output :
Addition of x and y is:30
Run : java Addition
Output :
Addition of x and y is:30
----------------------------------------------------------------------
Note :
- If we run above program 100 times also it displays the same output, because x and y values are hard-coded.
- In real time we should not hard-code the values.
- so, if we want to customize the output according to the inputs at run time, then we should learn I/O concept
Input : Data given to a program
Output : Data displayed as a result of program
InputStream : A stream which receives or read the data
OutStream : A stream which sends or write the data
System.in
- Its a InputStream object, which represents standard input device i.e keyboard.
- Its a PrintStream object, which represents the standard output device, i.e monitor
java.io
- This package contains predefined classes to read & write the data with the help of stream objects.
- InputStreamReader and BufferedReader classe's objects will help us to read the data from the keyboard.
- These two are the predefined classes in java.io package
Syntax:
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
Syntax:
BufferedReader bufferedReader= new BufferedReader(inputStreamReader);- By using BufferedReader object we can call corresponding methods to read the data from the keyboard
To read String Data
String str=br.readLine();
To read int value
int i=Integer.parseInt(br.readLine());
To read float value
float f=Float.parseFloat(br.readline());
-------------------------------------------------------------------
Program : To accept values at run time and adding
Program name : AdditionDemo1.java
Output :
Enter x value : 10
Enter y value : 30
Addition of x and y is : 40
import java.io.*;
class AdditionDemo1
class AdditionDemo1
{
public static void main(String args[]) throws Exception
{
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
System.out.println("Enter x value:");
int x=Integer.parseInt(bufferedReader.readLine());
System.out.println("Enter y value:");
int y=Integer.parseInt(bufferedReader.readLine());
System.out.println("Addition of x and y is:"+(x+y));
}
}
Compile : javac AdditionDemo1.java
Run : java AdditionDemo1
Output :
Enter x value : 10
Enter y value : 30
Addition of x and y is : 40
Run : java AdditionDemo1
Output :
Enter x value : 60
Enter y value : 40
Addition of x and y is : 100
-------------------------------------------------------------------
Program : To print Employee details
Program name : EmployeeDemo1.java
Output :
Enter employee id: 1
Enter employee name : Nireekshan
Enter employee salary : 40
Employee id: 2
Employee Name: Sravan
Employee salary: 50
import java.io.*;
class EmployeeDemo1
{
public static void main(String args[])throws IOException
{
InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
System.out.println("Enter employee id:");
int id=Integer.parseInt(br.readLine());
System.out.println("Enter employee name : ");
String name=br.readLine();
System.out.println("Enter employee salary : ");
float sal=Float.parseFloat(br.readLine());
System.out.println();
System.out.println("Employee id:"+id);
System.out.println("Employee Name:"+name);
System.out.println("Employee salary:"+sal);
}
}
Compile : javac EmployeeDemo1.java
Run : java EmployeeDemo1
Output :
Enter employee id : 1
Enter employee name : Nireekshan
Enter employee salary : 40
Employee id : 2
Employee Name : Sravan
Employee salary : 50
-------------------------------------------------------------------
java.util.Scanner class
- We can use Scanner class of java.util package to read input data from the keyboard.
Methods:
- next() - To read a String
- nextByte() - To read byte value
- nextInt() - To read integer value
- nextFloat() - To read float value
- nextLong() - To read long value
- nextDouble() - To read double value
-------------------------------------------------------------------
Program : To print Employee details
Program name : EmployeeDemo2.java
Output :
Enter employee id: 1
Enter employee name : Amar
Enter employee salary :25000
Employee id: 2
Employee Name: Hari
Employee salary: 35000
import java.util.*;
class EmployeeDemo2
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter employee id :");
int id=sc.nextInt();
System.out.print("Enter employee name :");
String name=sc.next();
System.out.print("Enter employee salary :");
float sal=sc.nextFloat();
System.out.println();
System.out.println("Employee id :"+id);
System.out.println("Employee name :"+name);
System.out.println("Employee salary :"+sal);
}
}
Compile : javac EmployeeDemo2.java
Run : java EmployeeDemo2
Output :
Enter employee id: 1
Enter employee name : Amar
Enter employee salary :25000
Employee id: 1
Employee Name: Hari
Employee salary: 35000
-------------------------------------------------------------------
Nireekshan