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) throws IOException
This method reads up to b.length bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

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

    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){
            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 : MAHENDRASINGH

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

HOME | COPYRIGHT | CONTACT US