Anda di halaman 1dari 8

J Soft

a J Soft Free E-knowledge Distribution Project Publication and Distribution

Java – Streams and Files


a short notes on Streams and Files in Java

Jeevan Kumar Vishwakarman


vishnudhoodhan@gmail.com

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...

Monday, November 29, 2010, 9:44:48 PM

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

Binary Files Text Files Random Access Sequential Access

 In Binary files the information are stored as the sequence of bytes.


 In Text file the information are stored as sequence of texts.

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.

Streams from file for reading, is called Input Streams


File
0 0 1 1 1 0 1 0 1 0 0 0 1 1 0 1 Program

Streams to file for writing, is called Output Streams

Stream ( a sequence of bits )

-------------------------------------------------------------------------------------------------------------------------------------------
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));

// Reading the file name using BufferedReader object ‘br’


System.out.println(“Enter the File Name : “);
String fname = br.readLine();

// creating file stream object to read from it


FileInputStream fis = new FileInputStream(fname);

// clearing the BufferedReader object’s memory


br = null
/* filling the BufferedReader object again with
the bits received from the file stream. */
br = new BufferedReader(new InputStreamReader(fis));

String s;

/* looping through all lines in the file.


and displaying it in the console.*/
while((s = br.readLine()!=null)
System.out.println(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.

Direct Method Buffered Method

Information Information

FileInputStream FileInputStream

BufferedInputStream

Processing Instructions Processing Instructions

 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.

Both the read() and write() functions throws an IOException

-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 4 Java – Streams and Files
Binary File Manipulation Examples

/* Writing to a binary file using direct method*/


import java.io.*;
class DirectBinaryWriter
{
public static void main(String args[]) throws IOException
{
/* filename is read from the commandline and passed to
FileOutputStream object */
FileOutputStream fos = new FileOutputStream(args[0]);
// looping 1 to 10
for(int i=1;i<=10;i++)
// writing 1 to 10 into the file
fos.write(i);
// manually closing the FileOutputStream to free memory
fos.close();
}
}

/* Writing to a binary file using Buffered method*/


import java.io.*;
class BufferedBinaryWriter
{
public static void main(String args[]) throws IOException
{
/* filename is read from the commandline and passed to
FileOutputStream object */
FileOutputStream fos = new FileOutputStream(args[0]);
/* creating a BufferedOutputStream object and passing the information
from the FileOutputStream object */
BufferedOutputStream bos = new BufferedOutputStream(fos);
for(int i=1;i<=10;i++)
// writing 1 to 10 into the file through the BufferedOutputStream
bos.write(i);
// manually closing the BufferedOutputStream to free memory
bos.close();
// manually closing the FileOutputStream to free memory
fos.close();
}
}

-------------------------------------------------------------------------------------------------------------------------------------------
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();
}
}

/* Reading a binary file in Buffered method */


import java.io.*;
class BufferedBinaryReader
{
public static void main(String args[])
{
// creating FileInputStream and passing file name through the commandline
FileInputStream fis = new FileInputStream(args[0]);
// Creating BufferedInputStream to store from FileInputStream
BufferedInputStream bis = new BufferedInputStream(fis);
// 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=bis.read())!=-1)
// converting the integer to character and printing it in console
system.out.println((char)i);
// closing the BufferedInputStream
bis.close();
// closing the FileInputStream
fis.close();
}
}

-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 6 Java – Streams and Files
Text File Manipulation Examples

/* Writing to a text file using Direct Method */


import java.io.*;
class DirectTextWriter
{
public static void main(String args[]) throws IOException
{
// creating FileWriter Object and initializing
FileWriter fw = new FileWriter(args[0]);
// looping through 1 to 10
for(int i=1;i<=10;i++)
// writing to text file as text
fw.write("Line " + i + "\n");
// closing the stream
fw.close();
}
}

/* Writing to a text file using Direct Method */


import java.io.*;
class BufferedTextWriter
{
public static void main(String args[]) throws IOException
{
// creating FileWriter Object and initializing
FileWriter fw = new FileWriter(args[0]);
// creating BufferedWriter object and initializing with FileWriter object
BufferedWriter bw = new BufferedWriter(fw);
// looping through 1 to 10
for(int i=1;i<=10;i++)
// writing to text file as text
bw.write("Line " + i + "\n");
// closing the streams
bw.close();
fw.close();
}
}

-------------------------------------------------------------------------------------------------------------------------------------------
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();
}
}

/* Reading to a text file using Direct Method */


import java.io.*;
class BufferedTextReader
{
public static void main(String args[]) throws IOException
{
// creating FileReader Object and initializing
FileReader fr = new FileReader(args[0]);
// creating BufferedReader Object and initializing with FileReader object
BufferedReader br = new BufferedReader(fr);

String s; // for storing read textline


while((s=br.readLine()!=null) // while EOF
// Writing text to screen
System.out.println(s);
// closing the stream
br.close();
fr.close();
}
}

-------------------------------------------------------------------------------------------------------------------------------------------
Jeevan Kumar Vishwakarman 8 Java – Streams and Files

Anda mungkin juga menyukai