file stream
input stream
output stream
input stream reads data from the source
output stream writes data to the destination
advantages
disadvantages
examples for both the input and output stream
3. FILES:
It is a collection of data
In which collection of informations are
stored.
4. STREAM:
A stream is a sequence of data. In Java, a
stream is composed of bytes. It's called a
stream because it is like a stream of water
that continues to flow.
5. Cntd….
In Java, 3 streams are created for us
automatically. All these streams are attached with
the console.
1) System.out : standard output stream
2) System.in: standard input stream
3) System.err : standard error stream
6. TYPES OF STREAMS:
There are two types of streams:
1. Input Stream – It reads data from the
source.
2. Output Stream – It writes data to the
destination.
7. INPUT STREAM
Input Stream class is the superclass of all the
io classes i.e. representing an input stream
of bytes. It represents input stream of bytes.
Applications that are defining subclass of
Input Stream must provide method, returning
the next byte of input.
A reset() method is invoked which re-
positions the stream to the recently marked
position.
8. EXAMPLE
import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("D:testout.txt
");
int i=fin.read();
System.out.print((char)i);
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
9. OUTPUT STREAM
This abstract class is the superclass of all
classes representing an output stream of
bytes. An output stream accepts output bytes
and sends them to some sink.
Applications that need to define a subclass of
OutputStream must always provide at least a
method that writes one byte of output.
10. EXAMPLE
import java.io.*;
class OutputStreamDemo
{
public static void main(String args[])throws Exception
{
OutputStream os = new FileOutputStream("file.txt");
byte b[] = {65, 66, 67, 68, 69, 70};
os.write(b);
os.flush();
for (int i = 71; i <75 ; i++)
{
os.write(i);
}
os.flush();
os.close();
}
}
11. RANDOM ACCESS FILE:
Files in which records can be accessed
in any order.it is also called direct
access file.