java fileinputstream byte code example

Example 1: java fileinputstream

try{    
            FileInputStream fin=new FileInputStream("D:\\testout.txt");    
            int i=0;    
            while((i=fin.read())!=-1){    
             System.out.print((char)i);    
            }    
            fin.close();    
          }catch(Exception e){System.out.println(e);}

Example 2: fileinputstream read(byte[] b) example

package com.technicalkeeda;
import java.io.FileInputStream;
import java.io.IOException;
public class App {     
  public static void main(String[] args) {        
    try {  
      FileInputStream fileInputStream = new FileInputStream("C:\\technicalkeeda\\hello.txt");             
      byte[] bytes = new byte[16];           
      int i = fileInputStream.read(bytes);             
      System.out.println("Total bytes read :- " + i);             
      for (byte b: bytes) {                
        char c = (char) b;                
        System.out.print(c);            }             
      fileInputStream.close();  
    } 
    catch (IOException e) {           
      e.printStackTrace();       
    }   
  }
}

//  hello.txt contents is:
//  Hello TechnicalKeeda

//  output is: 
//  Total bytes read :- 16
// 
//  Hello TechnicalK

Tags:

Java Example