OOPJ 4341602 Unit-5 2023
OOPJ 4341602 Unit-5 2023
Types of Stream
In general, a Stream will be an input stream (source) or, an output stream (destination).
o InputStream − used to read data from a source.
o OutputStream − used to write data to a destination.
Based on the data they handle there are two types of streams
1. Byte Stream Classes: These handle data in bytes (8 bits) i.e., the byte stream classes read/write
data of 8 bits. Using these you can store characters, videos, audios, images etc.
2. Character Stream Classes: Using these you can read and write text data only.
Syntax Description
It reads bytes of data from a stream. If no data is available it blocks the method. When a
read( ) method is blocked, the thread executing waits until the data is available. It rows an
IOExceptionif an error occurs.
available( ) This method returns the number of available bytes that can be read without blockage.
This method closes the input stream. It releases the resources associated with the stream. It
close( )
throws an IOException if an error occurs.
mark( ) This method is used to mark the current position in the stream.
This method repositions the stream to the last marked position. This method throws an
reset( )
IOExceptionif an error occurs.
This method skips n bytes of input.
skip( ) -n is the parameter used which specifies the number of bytes to be skipped. It throws an
IOException if an error occurs.
OutputStream Class
Java application uses an output stream to write data to a destination, it may be a file, an array
peripheral device or socket.
Like an input stream, an output stream is automatically opened when you create an object of
OutputStream class.
You can explicitly close an output stream with the close( ) method, or let it be closed implicitly when the
object is garbage collected.
Syntax Description
This method writes a byte and throws an IOException if an error occurs. This method actually
write()
blocks until the byte is written. The thread waits until the write operation is been completed.
This method flushes the stream. The buffered data is written to the outputstream. It throws an
flush()
IOException if an error occurs.
This method closes the stream. It is used to release any resource associated with the stream. It
close()
throws an IOException if an error occurs.
Write a java program to create a text file and perform write operation on the text file. 07 –
Winter-2023, Summer-2024
Example,
import java.io.*;
public class FileOutExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("d:/JAVAProgram/file1.txt");
String s="Welcome to OOPJ";
byte b[]=s.getBytes(); //converting string into byte array
fout.write(b);
fout.close();
System.out.println("Write in file successfully.");
}
catch(Exception e){ System.out.println(e); }
}
}
Output: Write in file successfully.
FileInputStream class
FileInputStream is used for reading data from the files.
While using FileInputStream class, file must already exist if file does not exist it will generate error.
There are two way for using file with FileInputStream.
FileInputStream f1 = new FileInputStream("D:/java/hello.txt");
OR
Write a java program to create a text file and perform read operation on the text file. 07 – Winter-
2023, Summer-2023(03)
Example,
import java.io.*;
class FileInExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("d:/JAVAProgram/FileHandling/file2.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}
catch(Exception e){System.out.println(e);}
}
}
Output: Welcome to OOPJ
Character Streams
FileWriter
Java FileWriter class is used to write character data to the file.
Two ways to create FileWriter Object.
FileWriter f = new FileWriter(String fname); //File path is specified in argument.
OR
FileWriter f = new FileWriter(File f1); //1st file is created and it is passed as argument.
Method of FileWriter Class
FileReader
Java FileReader class is used to read character data from the file.
Two ways to create FileReader Object.
FileReader f = new FileReader(String name); //File path is specified in argument.
OR
FileReader f = new FileReader(File f1); //1st file is created and it is passed as argument.
Method of FileReader Class
int read() Returns a character in ASCII form. It returns -1 at the end of file.
int read(char[] ch) To read data from the file into char array.
void close() Closes FileReader.
Write a java program to perform read operation on the text file using FileReader class. 04
Example,
import java.io.*;
class FileReaderExample {
public static void main(String args[]){
try{
FileReader r=new FileReader("d:/JAVAProgram/file1.txt");
int i=0;
while((i=r.read())!=-1){
BufferedWriter - BufferedWriter class is used to provide buffering for Writer instances. It makes the
performance fast. It inherits Writer class. The buffering characters are used for providing the efficient
writing of single arrays, characters, and strings.
Example,
import java.io.*;
class BufferedWriterExample {
public static void main(String args[]) throws Exception{
FileWriter writer = new FileWriter("D:/JAVA/FileHandling/file1.txt");
BufferedWriter b = new BufferedWriter(writer);
b.write("Welcome to java.");
b.close();
System.out.println("Write Successfully");
}
}
Output: Write Successfully
Buffered Reader - 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.
Example,
import java.io.*;
public class BufferedReaderExample {
public static void main(String args[]){
try{
FileReader r=new FileReader("d:/JAVA/FileHandling/file1.txt");
BufferedReader br=new BufferedReader(r);
int i=0;