Java FilterInputStream read() Example

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


Syntax is : public int read(byte[] b) throws IOException
Method reads up to byte.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available. This method simply performs the call read(b, 0, b.length) and returns the result.

Here is the code.
/**
 * @(#) Read1FilterInputStream.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 Read1FilterInputStream extends FilterInputStream {

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

  public static void main(String[] argsthrows IOException{

        // Declare the buffer and initialize its size:
        byte[] arrByte = new  byte[1024];
    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);
    Read1FilterInputStream obj =new Read1FilterInputStream(objFin);

        System.out.print("Content of the file : ");
        // Read from the buffer one character at a time:
      int c=0;
        while ((c = obj.read(arrByte)) >= 0){
            for (int i = 0; i < c; i++) {

            // Display the read byte:
            System.out.print((char)arrByte[i]);
      }
    }
  }
}

Output of the program.
 Content of the file : MAHENDRA

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

HOME | COPYRIGHT | CONTACT US