Chapter 7
Chapter 7
7 Input/Output Stream
I/O Stream
Java perform I/O through stream. Java i/o means input/output provided by the
java.io package.
7.1 Stream
Stream is any input source (or) output destination for data. And stream is linked to a
physical device by java i/o stream by
i/p devices like keyboard, mouse.
o/p devices like screen, printer.
Java's IO package mostly concerns itself with the reading of raw data from a source
and writing of raw data to a destination.
The below diagram illustrates the principle of a program reading data from a source
and writing it to some destination:
A program that needs to read data from some source needs an input stream or Reader.
A program that needs to write data to some destination needs an output stream or writer.
This is also illustrated in the diagram below:
System.out is a PrintStream. System.out normally outputs the data you write to it to the
console.
Object
InputStream
SequenceInput
FileInput Stream
Stream
PipeInput ObjectInput
Stream Stream
ByteArrayInput StringBufferedInput
Stream Stream
Filterinput
Stream
PushbackInput
BufferedInput
Stream
Stream
DataInput
Stream
DataInput
Data Method
Description
Type
void close() Closes this output stream and releases any system
resources associated with this stream.
void flush() Flushes this output stream and forces any buffered output
bytes to be written out.
void write(byte[] b Writes b.length bytes from the specified byte array to
)
this output stream.
void write(byte[] b
, int off,
Writes len bytes from the specified byte array starting at
int len) offset off to this output stream.
Input/Output Stream 7.5
abstrac write(int b)
t void Writes the specified byte to this output stream.
Table 7.2 Methods provide by Output Stream Classes
Object
OutputStream
PipeOutput Stream
ByteArrayOutputStream
Filteroutput
Stream
Dataoutput
Reader stream classes are used to read character from the files. Reader class is the
base class for all other classes, reader stream classes uses characters as a functional unit
of information
Object
Reader
StringReader
BufferedReader
Classe
PipeReader
CharArrayReader
InputStreamReader FilterReader
FileReader PushbackReader
Object
Writer
PrintWriter
BufferedWriter
CharArrayWriter StringWriter
FilterWriter PipeWriter
OutputStreamWriter
FileWriter
String getEncoding() Return the name of the character encoding being used by
this stream.
void write(char[] c
buf, int off, Write a portion of an array of characters.
int len)
void write(String st
r, int off, Write a portion of a string.
int len)
Output
D:\my book\io stream>java CharReader
Enter the characters and type . to stop reading
java.io
Input/Output Stream 7.9
j
a
v
a
.
import java.io.*;
public class StringReader
{
public static void main (String[] args)
{
System.out.println("Type some text and press 'Enter.'");
String string = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
try
{
string = reader.readLine();
}
catch(Exception e){}
System.out.println("You typed: " + string);
System.out.println("Type 'Enter' to exit.");
try
{
string = reader.readLine();
}
catch(Exception e){System.out.println(e);}
}
}
Program 7.2 To read String
Output:
D:\my book\io stream>java StringReader
Type some text and press 'Enter.'
wel come to java program.
You typed: wel come to java program.
Type 'Enter' to exit.
import java.io.*;
class CreateFile
{
7.10 Java Programming Paradigms
Output
import java.io.*;
class Write
{
public static void main(String args[])
{
try
{
FileOutputStream fos=new FileOutputStream("Write.txt");
String s= "welcome to java Program";
byte b[]=s.getBytes();
fos.write(b);
fos.close();
System.out.println("Successfully saved");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Program 7.4 To write a file
Input/Output Stream 7.11
Output
The string content will be saved in the file.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerReadFile
{
public static void main(String[] args)
{
File file = new File("Write.txt");
try
{
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Program 7.5 To Reading a file using Scanner
Output
import java.io.*;
class FileRead
{
public static void main(String args[])
{
try{
7.12 Java Programming Paradigms
Review Questions
Part-A (2 marks)
1) What is a stream and which class allows you to read objects directly from a
stream?
2) What are the methods provided by inputstream classes?
3) What are the methods provided by outputstream classes?
Part-B
1) Explain IO Stream with example programs.
2) Write a program to read a character.
3) Write a program to read a string.
4) Write a program to create a file.
5) Write a program to write the content in the file.
6) Write a program to read a file using FileInputStream.
7) Write a program to reading a file using Scanner.