Sunday, May 12, 2013

Java File Input Stream Example

Java File Input Stream Example

We can read the files content by using File Input Stream also.

File Input Stream  reads the stream of raw bytes.

File Input Stream reads the data by using the read() method.

File input Stream Example:


 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 //Read the file content using File InputStream.  
 public class FileInputStreamExample {  
       public static void main(String[] args) {  
            FileInputStream fis = null;//<i>File Input Stream</i>   
           File fileName = null;  
            try {  
                 fileName = new File("C:\\core.txt");  
                 fis = new FileInputStream(fileName);  
                 int c;  
                 while ((c = fis.read()) != -1) {  
                     System.out.print((char) c);  
                }  
           } catch (IOException e) {  
                e.printStackTrace();  
           } finally {  
                try {  
                     if (fis != null){  
                          fis.close();  
                          fis = null;  
                     }  
                } catch (IOException ex) {  
                     ex.printStackTrace();  
                }  
           }  
      }  
 }  


0 comments:

Post a Comment

 
Disclaimer : If you find any mistakes / corrections / feedback Please let us know...This website author shall not accept liability or responsibility for any errors, mistakes or misstatements found in this website.