Pin It

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 :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot