Module-IV (Part-III):
Input/Output
by:
Dr. Soumya Priyadarsini Panda
Sr. Assistant Professor
Dept. of CSE
Silicon Institute of Technology, Bhubaneswar
Input / Output
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 [Link] package contains all the classes required for input and
output operations.
File handling can be performed 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.
A stream can be defined as a sequence of data.
There are two types of Streams:
InputStream − The InputStream is used to read data from a source.
OutputStream − The OutputStream is used for writing data to a destination.
Byte Streams
Java byte streams are used to perform input and output of 8-bit bytes.
Most frequently used classes are:
FileInputStream
FileOutputStream.
Example
// copy an input file into an output file
import [Link].*;
public class CopyFile
{
public static void main(String args[]) throws IOException
{
FileInputStream in = null;
FileOutputStream out = null;
Cont…
try{
in=new FileInputStream("[Link]");
out =new FileOutputStream("[Link]");
int c;
while ((c = [Link]()) != -1)
{ [Link](c);
}
}
finally{
if (in != null) {
[Link]();
}
if (out != null) {
[Link]();
}}}}
Character Streams
Byte streams are used to perform input and output of 8-bit bytes,
whereas Java Character streams are used to perform input and output for
16-bit unicode.
Most frequently used classes:
FileReader
FileWriter.
Example
// copy an input file into an output file
import [Link].*;
public class CopyFile
{
public static void main(String args[]) throws IOException
{
FileReader in = null;
FileWriter out = null;
Cont…
try {
in = new FileReader("[Link]");
out = new FileWriter("[Link]");
int c;
while ((c = [Link]()) != -1) {
[Link](c);
}
}
finally {
if (in != null) {
[Link](); }
if (out != null) {
[Link]();
}}}}
Standard Streams
Java provides the following three standard streams −
Standard Input− This is used to feed the data to user's program and
usually a keyboard is used as standard input stream and represented
as [Link].
Standard Output − This is used to output the data produced by the
user's program and usually a computer screen is used for standard
output stream and represented as [Link].
Standard Error − This is used to output the error data produced by the
user's program and usually a computer screen is used for standard error
stream and represented as [Link].
Cont…
In Java, 3 streams are created for us automatically.
All these streams are attached with the console
1) [Link]: standard output stream
2) [Link]: standard input stream
3) [Link]: standard error stream
Cont…
Example:
[Link]("simple message");
[Link]("error message");
int i=[Link](); //returns ASCII code of 1st character
[Link]((char)i);//will print the character
Example
// creates InputStreamReader to read standard input stream until the user
types a “q”
import [Link].*;
public class ReadConsole
{
public static void main(String args[]) throws IOException
{
InputStreamReader cin = null;
Example
try {
cin = new InputStreamReader([Link]);
[Link]("Enter characters, 'q' to quit.");
char c;
do {
c = (char) [Link]();
[Link](c);
} while(c != 'q');
}
finally {
if (cin != null)
{
[Link]();
}}}}
Reading and Writing Files
Hierarchy of classes to deal with Input and Output streams:
FileInputStream
This stream is used for reading data from the files.
Objects can be created using the keyword new and there are several
types of constructors available.
Examples:
InputStream f = new FileInputStream("C:/java/hello");
Constructor takes a file name as a string to create an input stream object
to read the file
Cont…
Constructor takes a file object to create an input stream object to read
the file.
First create a file object
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
Methods
Example-1
import [Link];
public class DataStreamExample
{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("D:\\[Link]");
int i=[Link]();
[Link]((char)i);
[Link]();
}
catch(Exception e){[Link](e);}
}
}
Cont…
Output:
Before running the code, a text file named as "[Link]" is required to
be created. Let the file contains the text: “Welcome”
After executing the above program, you will get a single character from
the file which is 87 (in byte form).
To see the text, you need to convert it into character.
Example-2
import [Link];
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("D:\\[Link]");
int i=0;
while((i=[Link]())!=-1)
{
[Link]((char)i);
}
[Link]();
}catch(Exception e){[Link](e);}
}
}
FileOutputStream
Java FileOutputStream is used to create a file and write data into it.
The stream would create a file, if it doesn't already exist, before opening
it for output.
Two constructors can be used to create a FileOutputStream object.
Following constructor takes a file name as a string to create an input
stream object to write the file −
OutputStream f = new FileOutputStream("C:/java/hello")
Cont…
Following constructor takes a file object to create an output stream object
to write the file.
First, create a file object:
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
Methods
Example-1
import [Link];
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\[Link]");
[Link](65);
[Link]();
[Link]("success...");
}catch(Exception e){[Link](e);}
}
}
Output:
The content of a text file [Link] is set with the data
A.
Success…
Example-2
import [Link];
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("D:\\[Link]");
String s=“java programming";
byte b[]=[Link]();//converting string into byte array
[Link](b);
[Link]();
[Link]("success...");
}catch(Exception e){[Link](e);}
} }
Output:
The content of a text file [Link] is set with the data Java Programming
Success…
Java BufferedReader Class
BufferedReader class is used to read the text from a character-based
input stream.
It can be used to read data line by line by readLine() method.
It makes the performance fast.
It inherits Reader class.
Declaration:
[Link]
public class BufferedReader extends Reader
Cont…
Methods
Example-1
import [Link].*;
public class BufferedReaderExample
{
public static void main(String args[])throws Exception
{
FileReader fr=new FileReader("D:\\[Link]");
BufferedReader br=new BufferedReader(fr);
int i;
while((i=[Link]())!=-1){
[Link]((char)i);
}
[Link]();
[Link]();
}
}
Example-2
import [Link].*;
public class BufferedReaderExample
{
public static void main(String args[])throws Exception
{
InputStreamReader r=new InputStreamReader([Link]);
BufferedReader br=new BufferedReader(r);
[Link]("Enter your name");
String name=[Link]();
[Link]("Welcome "+name);
}
} Output:
Enter your name
Amit
Welcome Amit