Java FileInputStream read() Example

FileInputStream 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.

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

public class Read2FileInputStream {

    public static void main(String[] argsthrows IOException {
    
      // Declare the buffer and initialize its size:
        byte[] arrByte = new  byte[1024];

      // create object of File class 
    File file = new File("Mahendra.txt");
    byte[] b;
       b = new byte[]{'M','A','H','E','N','D','R','A',' ','S','I','N','G','H'};
        FileOutputStream objFileOutStream = new FileOutputStream(file);
        objFileOutStream.write(b);
        
        // Create object of FileInputStream class.
    FileInputStream objFileInStream = new FileInputStream(file);

        System.out.print("Content of the file : ");
        // Read from the buffer one character at a time:
      int c=0;
        while ((c = objFileInStream.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 SINGH

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

HOME | COPYRIGHT | CONTACT US