A software stream is nothing but the flow of the data.
Every I/O method throws “checked exceptions”.
We can perform read and write operations on the stream objects.
Steps to perform I/O operations :
1.Create the appropriate stream object.
2.Perform the read/write operation.
3.Close the stream.
We can perform read and write operations on the stream objects.
Steps to perform I/O operations :
1.Create the appropriate stream object.
2.Perform the read/write operation.
3.Close the stream.
Device independence is achieved thru streams.
Java.io package provides the necessary library support for I/O programming.
Byte Streams : Input streams, Output streams……
DataInputStream – DataOutputStream for reading from the keyboard.
//reading data from keyboard
DataInputStream dis=new DataInputStream(System.in);
String name=dis.readLine();
FileInputStream – FileOutputStream for reading and writing to a file,throws FileNotFound exception.
//read from a file
FileInputStream fis=new FileInputStream(args[0]);
Int c=fis.read();
//write to a file
FileOutputStream fos=new FileOutputStream(“test2.txt”)
byte b[]=str.getBytes();
fos.write(b);
BufferedInputStream – BufferedOutputStream to increase the performance of reading and writing to a file.
FileInputStream fis=new FileInputStream(args[0]);
BufferedInputStream bis=new BufferedInputStream(fis);
int c=bis.read();
SequenceInputStream is used to read 2 files sequentially.
FileInputStream fis1=new FileInputStream(“file1”);
FileInputStream fis2=new FileInputStream(“file2”);
SequenceInputStream sis=new SequenceInputStream(fis1,fis2);
ObjectInputStream – ObjectOutputStream are used to store and retrieve the object to and from the file.
OOS uses “writeObject()” to write the object into a file.
OIS uses “readObject()” to return the references of the objects.
//reading an object from a file
FileInputStream fis=new FileInputStream(“emp1.dat”);
ObjectInputStream ois=new ObjectInputStream(fis);
rone=(emp)ois.readObject();
rone.display();
//writing an object to a file
emp e= new emp();
FileOutputStream fos=new FileOutputStream(“emp1.dat”);
ObjectOutputStream oos=new ObjectOutputStream(fos);
Oos.writeObject(e);
If we want to store an object state into a file, the class must implement
java.io.Serializable interface.
The process of converting an object into a stream so that its state can be stored into a file is known as “Serialization”.
Logic to copy the contents of one file into another…
FileInputStream fis=new FileInputStream(args[0]);
FileOutputStream fos=new FileOutputStream(args[1]);
int i=fis.read();
while(i!=-1)
{
fos.write(i);
i=fis.read();
}
Character Streams : Reader and Writer…….
System.in is static, and it gives byte oriented streams.
InputStreamReader converts the byte oriented stream into character oriented stream.
BufferedReader is used to read the data from the keyboard.BuffererReader constructor cannot directly accept byte oriented stream as parameter.Therefore we use the bridge class InputStreamReader to convert into character oriented stream.
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name=br.readLine();
FileReader and FileWriter are used to read and write the character oriented stream from and to a file.
//reading from a file
FileReader fr=new FileReader(args[0]);
int i=fr.read();
//writing to a file
FileWriter fw=new FileWriter(“abc.txt”);
String str=”hai venu”;
Fw.write(str);
RandomAccessFile is a class in java.io which is used to read the contents of a file from any location.This is also used to append or insert the text to the existing file.As this RAF is able to perform both read and write operations, we need to mention the file mode.
RandomAccessFile raf=new RandomAccessFile(“file”,r/rw);
Raf.read();
Methods in RAF :
seek(),getFilePointer(),read(),close(),write(),length()
Pre-defined Streams : java.lang.System class has 3 class(static) variables.
in----InputStream----represents keyboard.
out---PrintStream----represents console.
err---PrintStream----represents console.
What is the difference between the File and RandomAccessFile classes?
The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods needed to directly access data contained in any part of a file.
Byte Streams
InputStream--is an abs class that defines java's model of streaming byte input.
OutputStream--is an abs class that defines streaming byte
output.
FileInputStream--creates an input stream that u can use to read bytes from a file.
FileOutputStream--creates an output strean that u can use to write bytes to a file.
ByteArrayInputStream--is an implementation of an input stream that uses a byte array as the source.
ByteArrayOutputStream--is an implementation of an output stream that uses a byte array as the destination.
FilteredByte Streams--FilterInputStream,FilterOutputStream.
BufferedByteStreams--BufferedInputStream,BufferedOutputStream,PushbackInputStream.
Charecter Streams
FileReader--creates a reader that u can use to read the contents of a file.
FileWriter--creates a writer that u can use to write to a file.
CharArrayReader--is an implementation of an input stream that uses a charecter array as a source.
CharArrayWriter--is an implementation of an output stream that uses a character array as a destination.
Bufferedreader--improves performance by buffering input.
bufferedWriter--is a writer that add a flush() that can be used to ensure that data buffers are physically written to the actual output stream.
PrintWrite--is essentially a char-oriented version of PrintStream
--provides the formatted output methods print(),println().