Program to demonstrate EOFException and a way to detect end of file in Java.
Output of the program :
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 :