KEMBAR78
CH 9 FileStream | PDF | String (Computer Science) | Text File
0% found this document useful (0 votes)
8 views8 pages

CH 9 FileStream

Uploaded by

manish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

CH 9 FileStream

Uploaded by

manish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CREATIVE DESIGN & MULTIMEDIA INSTITUTE

Stream & Types In Java


Java encapsulates Stream under java.io package. Java defines
two types of streams. They are,
 Byte Stream : It provides a convenient means for handling
input and output of byte.
 Character Stream : It provides a convenient means for
handling input and output of characters.

RandomAccessFile in Java
 The Java RandomAccessFile class in the Java IO API allows you to move
navigate a file and read from it or write to it as you please.
 You can replace existing parts of a file too. This is not possible with the
FileInputStream or FileOutputStream.
 Access Modes:

Mode Description

r Read mode. Calling write methods will result in an IOException.

rw Read and write mode.

 Seeking in a RandomAccessFile

To read or write at a specific location in a RandomAccessFile you must first


position the file pointer at (seek) the position to read or write. This is done
using the seek() method.

RandomAccessFile file = new RandomAccessFile("c:\\data\\file.txt", "rw");

file.seek(200);
CREATIVE DESIGN & MULTIMEDIA INSTITUTE

Example: //Data write in file

String data="hello this is test";


RandomAccessFile file = new RandomAccessFile("D:\\Input-File.txt", "rw");
file.write(data.getBytes());
file.close();

//Data Read from File with specific position

RandomAccessFile file=new RandomAccessFile("d:\\one.txt", "r");


byte b[]=new byte[1024];
file.seek(5); // read data start with 5th position
file.read(b);
str=new String(b);
System.out.println(str);
file.close();

CharacterStream Classes in Java


 CharacterStream classes to overcome the limitations of ByteStream
classes, which can only handle the 8-bit bytes and is not
compatible to work directly with the Unicode characters.
 CharacterStream classes are used to work with 16-bit Unicode
characters. They can perform operations on characters, char
arrays and Strings.
 CharacterStream classes are mainly used to read characters from
the source and write them to the destination.
 CharacterStream classes are divided into two types of classes, I.e.,
Reader class and Writer class.
CREATIVE DESIGN & MULTIMEDIA INSTITUTE

1. Java FileReader Class


 Java FileReader class is used to read data from the file.
Method Description
int read() It is used to return a character in ASCII form. It returns -1 at the end of file.
void close() It is used to close the FileReader class.

Example:
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\testout.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}

2. Java FileWriter Class


 Java FileWriter class is used to write character-oriented data to a
file.
 you don't need to convert string into byte array because it
provides method to write string directly.

Example:

public static void main(String args[]){


try{
FileWriter fw=new FileWriter("D:\\testout.txt");

fw.write("Welcome to CDMI.");

fw.close();

}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
}
CREATIVE DESIGN & MULTIMEDIA INSTITUTE

3. Java BufferedReader Class


 Java BufferedReader class is used to read the text from a character-
based input stream.
 It makes the performance fast.
Method Description
int read() It is used for reading a single character.
String readLine() It is used for reading a line of text.
void close() It closes the input stream and releases any of the system resources
associated with the stream.
Example:
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D:\\testout.txt");

BufferedReader br=new BufferedReader(fr);

int i;
while((i=br.read())!=-1){

System.out.print((char)i);

br.close();

fr.close();
}
CREATIVE DESIGN & MULTIMEDIA INSTITUTE

4. Java BufferedWriter Class


 Java 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:

public static void main(String[] args) throws Exception {


FileWriter writer = new FileWriter("D:\\testout.txt");
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write("Welcome to CDMI.");
buffer.newLine();
buffer.write("Good morning.");
buffer.close();
System.out.println("Success");
}
}

Bytestream Classe In Java


1. InputStream & FileInputStream
 Java FileInputStream class obtains input bytes from a file.
 It is used for reading byte-oriented data (streams of raw bytes)
such as image data, audio, video etc.
 You can also read character-stream data. But, for reading streams
of characters, it is recommended to use FileReader class.
CREATIVE DESIGN & MULTIMEDIA INSTITUTE

Example of read all characters

public static void main(String args[]){


try{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}catch(Exception e){System.out.println(e);}
}

2.OutputStream & FileOutputStream


 Java FileOutputStream is an output stream used for writing data to
a file
 for character-oriented data, it is preferred to use FileWriter than
FileOutputStream.

Example of write string


public static void main(String args[]){

try{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");

String s="Welcome to CDMI.";


byte b[]=s.getBytes();//converting string into byte array

fout.write(b);

fout.close();
System.out.println("success...");

}catch(Exception e){System.out.println(e);} }
CREATIVE DESIGN & MULTIMEDIA INSTITUTE

3. DataOutputStream Class
 Java DataOutputStream class allows an application to write
primitive Java data types to the output stream in a machine-
independent way.

Example:
public class OutputExample {
public static void main(String[] args) throws IOException {
FileOutputStream file = new FileOutputStream(D:\\testout.txt);
DataOutputStream data = new DataOutputStream(file);
data.writeInt(65);
data.close();
System.out.println("Succcess...");
} }

4. DataInputStream Class

 Java DataInputStream class allows an application to read primitive data from


the input stream in a machine-independent way.

Example:
FileInputStream f=new FileInputStream("d:\\raj3.txt");
DataInputStream ds=new DataInputStream(f);
int a;
a=ds.readInt();
System.out.println(a); //read ascii value
System.out.println((char)a); //read character
ds.close();
CREATIVE DESIGN & MULTIMEDIA INSTITUTE

Piped Streams, Bridge Classes :

InputStreamReader
 An InputStreamReader is a bridge from byte streams to character streams
 It reads bytes and decodes them into characters using a specified charset. The
charset that it uses may be specified by name or the platform's default charset
may be accepted.

Example:
public static void main(String[] args) {

InputStream st=new FileInputStream("d:\\raj.txt");


Reader r=new InputStreamReader(st);
int i;

while((i=r.read())!=-1){
System.out.print((char)i);
}

OutputStreamWriter
 OutputStreamWriter is a class which is used to convert character stream to
byte stream.
 We optimize the performance of OutputStreamWriter by using it with in a
BufferedWriter so that to avoid frequent converter invocation.

Example:
public static void main(String[] args) {

OutputStream st=new FileOutputStream("d:\\raj5.txt");


Writer r=new OutputStreamWriter(st);
r.write("testing file ");
r.close();
}

You might also like