Advertisement
BufferedInputStream(InputStream is) |
---|
This constructor creates a BufferedInputStream object to read the data out of a file, which is accessed by an InputStream, passed to this constructor as a parameter. Example-:
|
Methods | Description |
---|---|
available() | This method gives the remaining number of bytes available to read from this input stream. |
read() | This method reads one byte at a time from this input stream. |
read(byte[]) | This method reads a whole byte array at a time from this input stream. |
close() | This method closes this input stream and also frees any resources connected with this input stream. |
Advertisement
Hello there. How are you doing?
//Java - Using BufferedInputStream to read(one byte at a time) out of an existing file.
import java.io.*;
class A
{
public static void main(String... ar)
{
try
{
FileInputStream fis= new FileInputStream("D:\\Textbook.txt");
BufferedInputStream bis= new BufferedInputStream(fis); //A local buffer is created which is big-
//enough to hold chunk of bytes read out of file TextBook.txt
//Determining the total number of bytes available to read
System.out.println("Bytes available to read = "+ bis.available());
int c;
while( (c=bis.read())!=-1) //reading local buffer(one byte at a time) until EOF is reached
System.out.print((char)c);
//Closing the input streams
bis.close();
fis.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Bytes available to read = 31
Hello there. How are you doing?
//Java - Using BufferedInputStream to read a byte array out of a file.
import java.io.*;
class A
{
public static void main(String...ar)
{
try
{
FileInputStream fin= new FileInputStream("D:\\TextBook.txt");
BufferedInputStream bis= new BufferedInputStream(fis); //A local buffer is created which is big-
//enough to hold chunk of bytes read out of file TextBook.txt
//Determining the total number of bytes available to read
int num= bis.available();
System.out.println("Number of bytes available to read =" + num);
byte b[]= new byte[num];
int c;
//read() method stops reading from file when EOF is reached i.e. -1
if((c=bis.read(b))!=-1)
System.out.println("Number of bytes read = " +c);
String s= new String(b);
System.out.println(s);
fin.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Number of bytes available to read =31
Number of bytes read = 31
Hello there. How are you doing?
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement