Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

EOFException : Program to detect end of file in Java

Program to demonstrate EOFException and a way to detect end of file in Java.

package com.hubberspot.exception.example;

import java.io.*;

public class EndOfFileDetection { 
  public static void main(String[] args) {
    try {
      DataOutputStream output = new DataOutputStream
        (new FileOutputStream("C://EndOfFile.dat"));
      output.writeChars("Welcome ") ;
      output.writeChars("to ") ;
      output.writeChars("Hubberspot !.") ;
      output.close();
      
      DataInputStream input = new DataInputStream
        (new FileInputStream("C://EndOfFile.dat"));
      while (true) {
        System.out.print(input.readChar());
      }
    }
    catch (EOFException ex) {
      System.out.println("");
      System.out.println("All data read");
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}


Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot