Dedicated To
Mr. P. Periyasamy
one of the Best Teacher I’ve ever seen, who is himself a keen student on what he
teaches, and made me fond in Java subjects and provided a lot real world knowledge,
on the Java. So, I solemnly with great gratitude I dedicate this eBook to him...
Prepared In Accordance with the Class Sessions taken and notes provided by
Mr. P. Periyasamy
Lecturer, Department of Computer Applications
Sree Saraswathi Thyagaraja College, Thippampatti, Pollachi, Tamizh Nadu, India
Streams And Files
A File Consists A Collection Of Information
Files
The Binary file is more savvy than the text format. for example the information “12345”
1. in Binary files, converted to binaries that is to integers, that only requires 4 bytes to store the
whole number.
2. but in Text files each five digits is stored as characters so, 5 x 2 bytes for each character, totally
requires a 10 bytes to store the whole number.
Streams
Streams are the sequence of the bits or bytes. Streams are required to stream (throw or
transport) the data into the file, and also to receive the content from a file. An object from which is used
to read a sequence of bytes is called input stream. Like the same the object to which the sequence of
bytes could be written is called the output stream.
-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 2 Java – Streams and Files
/* Simple File Manipulation Demo.
Reading a files content and displaying it on the console window */
import java.io.*;
class ReadFile
{
public static void main(String args[]) throws IOException
{
// creating an object to read file name from console
BufferedReader br = new BuffredReader(new InputStreamReader(System.in));
String s;
-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 3 Java – Streams and Files
Read Write Methods
there are two types of read or write methods. 1. Direct and 2. Indirect or Buffered Method
1. Direct Method
In this method the stream accesses the hard disk for each read or write instruction. This
causes high seek time, and makes the process comparatively slower and longer.
2. Buffered Method
In this method the streams are totally read at once at the start and stored into a buffer
memory. Then on each read or write instruction the buffer memory is referred and accessed
for the information. No hard disk is disturbed. As the buffer memory is faster than hard disk
seek time is reduced, and this buffer read or write makes the process comparatively faster.
Information Information
FileInputStream FileInputStream
BufferedInputStream
To read from a binary file in both direct and buffered methods, object of FileInputStream and its
read() function is used. In buffered method, additionally the object of BufferedInputStream class
is used, which receives the FileInputStream object as parameter to initialize.
To write the to the binary file in both direct and buffered methods, object of FileOutputStream
and its write() function is used. In buffered method, additionally the object of
BufferedOutputStream class is used, which receives the FileOutputStream object as parameter
to initialize.
To read from a text file, in both methods FileReader class object is used. In buffered method
additionally the BufferedReader class object is used, which receives the FileReader object as
parameter to initialize. To read, in direct method the read() function is used. In buffered method
readLine() is used.
To write to a text file, in both methods write() is used. And FileWriter and BufferedWriter
objects are used.
-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 4 Java – Streams and Files
Binary File Manipulation Examples
-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 5 Java – Streams and Files
/* Reading a binary file in Direct method */
import java.io.*;
class DirectBinaryReader
{
public static void main(String args[])
{
// creating FileInputStream and passing file name through the commandline
FileInputStream fis = new FileInputStream(args[0]);
// inputs will be binary. So declaring a int to store the read binary
int i;
// looping through file's each character. a -1 will indicate the EOF
while((i=fis.read())!=-1)
// converting the integer to character and printing it in console
system.out.println((char)i);
// closing the FileInputStream
fis.close();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 6 Java – Streams and Files
Text File Manipulation Examples
-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 7 Java – Streams and Files
/* Reading to a text file using Direct Method */
import java.io.*;
class DirectTextReader
{
public static void main(String args[]) throws IOException
{
// creating FileReader Object and initializing
FileReader fr = new FileReader(args[0]);
int i; // for storing read integer
while((i=fr.read()!=-1) // while EOF
// writing integer to screen after converting to char
System.out.println((char) i);
// closing the stream
fr.close();
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 8 Java – Streams and Files