- BufferedInputStream
- BufferedOutputStream
- DataInputStream
- DataOutputStream
1. BufferedInputStream : kind of inputStream that buffers the input data in order to optimize the speed access to the data.
2. BufferedOutputStream : kind of outputStream that buffers bytes.
3. DataInputStream : is a kind of InputStream used to read data as primitive data types from an underlying input stream.
4.DataOutputStream : kinf of outputStream that allows user to write data as primitive data types.
Examples:
BufferedInputStream
/*
* Example of Buffered Input Stream
*/
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class BufferedInputStreamExample {
public static void main(String[] args) {
//Get the file to read using BufferedInputStream
File file = new File("C://tutorialpointph//file.txt");
BufferedInputStream bin = null;
try
{
//obtains input bytes from the file
FileInputStream fin = new FileInputStream(file);
/*
* creates a BufferedInputStream and saves
* it into the buffer array for further use
*/
bin = new BufferedInputStream(fin);
//read the file
//available : while there are bytes to read
while( bin.available() > 0 ){
System.out.print((char)bin.read());
}
}
catch(FileNotFoundException e){
System.out.println("File not found on path" + e);
}
catch(IOException ie)
{
System.out.println("Exception while reading " + ie);
}
finally
{
//close the BufferedInputStream after reading
try{
if(bin != null)
bin.close();
}catch(IOException ie){
System.out.println("Error while closing : " + ie);
}
}
}
}
0 comments:
Post a Comment