Read file in byte array using FileInputStream - Java File Path IO

Java examples for File Path IO:FileInputStream

Description

Read file in byte array using FileInputStream

Demo Code


import java.io.*;
 
public class Main {
 
  public static void main(String[] args) {
    File file = new File("C://Folder//ReadString.txt");
    try//from w  ww . jav  a  2 s. com
    {
      FileInputStream fin = new FileInputStream(file);
     
      byte fileContent[] = new byte[(int)file.length()];
     
      fin.read(fileContent);
     
      String strFileContent = new String(fileContent);
     
      System.out.println("File content : ");
      System.out.println(strFileContent);
     
    }
    catch(FileNotFoundException e)
    {
      System.out.println("File not found" + e);
    }
    catch(IOException ioe)
    {
      System.out.println("Exception while reading the file " + ioe);
    }
  }
}
 

Result


Related Tutorials