Download presentation
Presentation is loading. Please wait.
1
Exception examples
2
import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream in) throws IOException { line = in.readLine(); if (line == null) throw new EOFException(); tokenizer = new StringTokenizer(line); }
3
public String readString(DataInputStream in) throws IOException { if (tokenizer == null) newline(in); while (true) { try { return tokenizer.nextToken(); } catch (NoSuchElementException exception) { newline(in); }
4
public double readDouble(DataInputStream in) throws IOException { if (tokenizer == null) newline(in); while (true) { try { String str = tokenizer.nextToken(); return Double.valueOf(str.trim()).doubleValue(); } catch (NoSuchElementException exception) { newline(in); }
5
public static void main (String[] args) { System.out.println("This is the Java IO Example"); IO test = new IO(); DataInputStream file = null; try { file = new DataInputStream(new FileInputStream( “ books.txt ” )); } catch (FileNotFoundException fnfe) { System.out.println( “ Could not find file. “ + “ Please place books.txt in main directory ” ); }
6
try { while (true) { System.out.println( “ Type: “ + test.readString(file)); System.out.println( “ Name: “ + test.readString(file)); System.out.println( “ Cost1: “ + test.readDouble(file)); System.out.println( “ Cost2: “ + test.readDouble(file)); } catch (EOFException exception) { //just exit the program } catch (IOException exception) { System.out.println( “ Exception occurred: “ + exception.getMessage()); } finally { System.out.println( “ This Line is printed anyhow. ” ); } } //close main } //close class
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.