Menu
Topics Index
...
`


Input/Output: Exploring java.io > Byte Streams >
Siva Nookala - 03 Mar 2017
InputStream is an abstract superclass of all classes representing an input stream of bytes. The subclassess of InputStream are AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream. These are used to read data in bytes.

public abstract class InputStream
extends Object
implements Closeable
Important Methods :
Method Description
abstract int read() This method reads the next byte of data from the input stream.
int read(byte[] b) This method reads some number of bytes from the input stream and stores them into the buffer array b.
void close() This method closes this input stream and releases any system resources associated with the stream.
Let's create a sample file fileOne with the absolute path E: JavaPrograms\FolderOne\fileOne.txt and write into it "This is a sample file". Now we will write a sample program on InputStream to read data from fileOne.
InputStreamDemo
import java.io.*;

class InputStreamDemo
{
    public static void main(String[] args) throws IOException
    {
        File f = new File("E:JavaPrograms\\FolderOne\\fileOne.txt"); //LINE A
    
        InputStream is = new FileInputStream(f); // LINE B
    
        if(f.exists()) // LINE C
        {
            System.out.println("File exists.");
            // LINE D
            int i = 0;
    
            // LINE E
            while((i = is.read()) != -1)  
            {
                System.out.print((char) i); // LINE F
            }
        }
        else
            System.out.println("File not found.");
        is.close(); // LINE G
    }
}
OUTPUT

File exists.
This is a sample file

DESCRIPTION

At LINE A we have given the path for file f.
At LINE B we have created an FileInputStream object and passed file f as parameter.
At LINE C we are checking whether file f exists or not.
At LINE D we have created a variable i to store the byte data from file f. read method always returns an integer of range 0 to 255
At LINE E we are assigning the i value to the byte value obtained from file f through InputStream.
At LINE F we are type casting int to char in order to print characters.
At LINE G we are closing the InputStream.

THINGS TO TRY
  • At LINE E change the data type of i to byte to see a compilation error. Since the return type of read method is int the compiler throws a compilation error.
RESET_CODE
  • At LINE Fremove the type casting and see the output. Integer values of range o to 255 are printed.
Rest of Methods :
Method Description
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.
void mark(int readlimit) This method marks the current position in this input stream.
boolean markSupported() This method tests if this input stream supports the mark and reset methods.
int read(byte[] b, int off, int len) This method reads up to len bytes of data from the input stream into an array of bytes.
void reset() This method repositions this stream to the position at the time the mark method was last called on this input stream.
long skip(long n) This method skips over and discards n bytes of data from this input stream.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App