Read file using FileInputStream - Java File Path IO

Java examples for File Path IO:FileInputStream

Description

Read file using FileInputStream

Demo Code

import java.io.*;
 
public class Main {
  public static void main(String[] args) {

    File file = new File("C://Folder//String.txt");
    int ch;//from   w  ww  .  j av a 2s  . c  om
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
   
    try{
      fin = new FileInputStream(file);
      while( (ch = fin.read()) != -1)
        strContent.append((char)ch);
      fin.close();
    } catch(FileNotFoundException e) {
      System.out.println("File " + file.getAbsolutePath() + " could not be found on filesystem");
    }catch(IOException ioe) {
      System.out.println("Exception while reading the file" + ioe);
    }
   
    System.out.println("File contents :");
    System.out.println(strContent);
  }
}

Result


Related Tutorials