Java FilterInputStream read() Example

FilterInputStream class read() method example. This example shows you how to use read() method.


Syntax is : public int read() throws IOException
Method reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

Here is the code.
/**
 * @(#) ReadFilterInputStream.java
 * A class representing use of method read() of FilterInputStream 
 class in java.io Package.
 * @Version  09-June-2008
 @author   Rose India Team
 */
import java.io.*;

public class ReadFilterInputStream extends FilterInputStream {

    ReadFilterInputStream(FileInputStream objFin){
    super(objFin);
    }

  public static void main(String[] argsthrows IOException{

    byte[] byteArray = new byte[]{'M','A','H','E','N','D','R','A'};

    //Create a file with a specified name and file extension.
    File file = new File("mahendra.txt");

    // Create a output stream and write specified bytes in file.
        FileOutputStream objFileStream = new FileOutputStream(file);
    objFileStream.write(byteArray);

    // create a file input stream to read content of the file.
      FileInputStream objFin = new FileInputStream(file);
    ReadFilterInputStream obj =new ReadFilterInputStream(objFin);

        System.out.print("Content of the file " + file + " : ");

    // read() method reads one by one element.
    System.out.print((char)obj.read());
        System.out.print((char)obj.read());
    System.out.print((char)obj.read());
    System.out.print((char)obj.read());
    System.out.print((char)obj.read());
    System.out.print((char)obj.read());
    System.out.print((char)obj.read());
    System.out.print((char)obj.read());
  }
}

Output of the program.
 Content of the file mahendra.txt : MAHENDRA

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US