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, int off, int len) throws IOException
Method reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned. This method simply performs in.read(b, off, len) and returns the result.

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

    Read2FilterInputStream(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);
    Read2FilterInputStream obj =new Read2FilterInputStream(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, arrByte.length)) >= 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