BufferedInputStream
My previous articles over Byte and character streaming use
unbuffered I/o . It means we are directly interacting with operating system for
each read and write request and make the program less efficient since for each
and every request lot of overhead is required which includes accessing of disk,
network activity etc.
To reduce this efforts , Java provides a functionality
,Buffered Input and Output Stream will read and write data from a memory known
as buffer .
In order to achieve this , here the unbuffered stream object
is passed to the constructor for a buffered stream class.
Example to use buffered I/O:
inputStream = new BufferedInputStream(new FileInputStream("file.txt"));
outputStream = new BufferedOutputStream(new FileOutputStream("newfile.txt"));
BufferedInputStream
BufferedInputStream will read the data from a memory known as buffer
Methods available:
1)
int
available() :This method returns an estimate of the number of bytes that
can be read (or skipped over) from this input stream without blocking by the
next invocation of a method for this input stream.
2)
void
close() :This method closes this input stream and releases any system
resources associated with the stream.
3) void mark(int readlimit) :This method
see the general contract of the mark method of InputStream.
ReadLimit -- number of bytes to be
read before the mark position gets invalid
4) boolean markSupported() :This method
tests if this input stream supports the mark and reset methods.
5)
int
read() :This method reads the
next byte of data from the input stream.
6)
int
read(byte[] b, int off, int len) :This method reads bytes from this
byte-input stream into the specified byte array, starting at the given offset.
Here b refers to a byte array to be populated ,off means starts storing from the offset and
len means number of bytes
to read.
7)
void
reset(): This method repositions this stream to the position at the time
the mark method was last called on this input stream.
8) long skip(long n) :This method skips
over and discards n bytes of data from this input stream
Example 1:
package bufferedistream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
/**
*
* @author shashank
*/
public class BufferedIStream {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
BufferedInputStream bis = null;
int available=0;
int i =0;
try
{
bis = new BufferedInputStream(new
FileInputStream("D:\\Articles\\filehandling\\FailedRecord.log"));
available = bis.available();
System.out.println("Available bytes:" + available);
while((i=bis.read())!=-1)
{
System.out.print((char)i);
}
bis.close();
}catch(Exception
e){e.printStackTrace();}
}
}
Example 2:
/*
* To change this license header, choose
License Headers in Project Properties.
* To change this template file, choose Tools |
Templates
* and open the template in the editor.
*/
package bufferedistream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
/**
*
* @author shabhatn
*/
public class BufferedIStream {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
BufferedInputStream bis = null;
int available=0;
int i =0;
Boolean bool;
try
{
bis = new BufferedInputStream(new
FileInputStream("D:\\Articles\\filehandling\\mark.txt"));
available = bis.available();
System.out.println("Available bytes:" + available);
bool = bis.markSupported();
System.out.println("Support for
mark() and reset() : "+bool);
System.out.println("Before
mark" + (char)bis.read());
System.out.println("Before
mark" + (char)bis.read());
System.out.println("Before
mark" + (char)bis.read());
bis.mark(1);//In other words,
readlimit is a suggestion; the stream is free to under-promise and over-deliver
System.out.println("Before
mark" + (char)bis.read());
bis.reset();
System.out.println("Before
mark" + (char)bis.read());
System.out.println("Before
mark" + (char)bis.read());
bis.reset();
System.out.println("Before
mark" + (char)bis.read());
System.out.println("Before
mark" + (char)bis.read());
bis.close();
}catch(Exception
e){e.printStackTrace();}
}
}
Example 3:
/*
* To change this
license header, choose License Headers in Project Properties.
* To change this
template file, choose Tools | Templates
* and open the
template in the editor.
*/
package bufferedistream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
/**
*
* @author shabhatn
*/
public class BufferedIStream {
/**
* @param args the
command line arguments
*/
public static void
main(String[] args) {
// TODO code
application logic here
BufferedInputStream
bis = null;
int
available=0;
int i =0;
Boolean bool;
try
{
bis = new
BufferedInputStream(new
FileInputStream("D:\\Articles\\filehandling\\mark.txt"));
available = bis.available();
System.out.println("Available bytes:" + available);
byte [] arr =
new byte[available];
bis.read(arr,3,1);
for(byte
b:arr)
{
System.out.print((char)b);
}
bis.close();
}catch(Exception e){e.printStackTrace();}
}
}
Example 4:
*/
package bufferedistream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
/**
*
* @author shabhatn
*/
public class BufferedIStream {
/**
* @param args the
command line arguments
*/
public static void
main(String[] args) {
// TODO code
application logic here
BufferedInputStream bis = null;
int
available=0;
int i =0;
Boolean bool;
try
{
bis = new
BufferedInputStream(new
FileInputStream("D:\\Articles\\filehandling\\mark.txt"));
available = bis.available();
System.out.println("Available bytes:" + available);
byte [] arr =
new byte[available];
bis.skip(2);
System.out.print((char)bis.read());
bis.close();
}catch(Exception e){e.printStackTrace();}
}
}




No comments:
Post a Comment