Mega Code Archive

 
Categories / Java / File Input Output
 

Reading Bytes from a DataInputStream

import java.io.DataInputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class EOF {   public static void main(String args[]) {     DataInputStream is = null;     byte ch;     try {       is = new DataInputStream(new FileInputStream("EOF.java"));       while (true) { // exception deals catches EOF         ch = is.readByte();         System.out.print((char) ch);         System.out.flush();       }     } catch (EOFException eof) {       System.out.println(" >> Normal program termination.");     } catch (FileNotFoundException noFile) {       System.err.println("File not found! " + noFile);     } catch (IOException io) {       System.err.println("I/O error occurred: " + io);     } catch (Throwable anything) {       System.err.println("Abnormal exception caught !: " + anything);     } finally {       if (is != null) {         try {           is.close();         } catch (IOException ignored) {         }       }     }   } }