Download presentation
Presentation is loading. Please wait.
Published byBob Ewell Modified over 6 years ago
1
Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, 10.2 9/25/13
2
Exceptions (Chapter 9.1) Exceptions help you take care of unexpected things, divide the program into normal case and a special case Exceptions are objects Exceptions are thrown Exceptions are handled 2
3
Exceptions Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction = (double) numerator / denominator ; System.out.println( fraction ); System.out.println( “End of program” ); 3
4
Exceptions Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction; if ( denominator != 0 ) { fraction = (double) numerator / denominator ; System.out.println( fraction ); } else { System.out.println( “You can’t divide by zero!” ); System.out.println( “Go learn about division!” ); } System.out.println( “End of program” ); 4
5
Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction; try { if ( denominator == 0 ) throw new Exception( “Exception! You can’t divide by zero”); fraction = (double) numerator / denominator ; System.out.println( fraction ); } catch ( Exception e ) { System.out.println( e.getMessage() ); System.out.println( “Go learn about division!” ); } System.out.println( “End of program” ); 5
6
Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction; try { if ( denominator == 0 ) throw new Exception( “Exception! You can’t divide by zero”); fraction = (double) numerator / denominator ; System.out.println( fraction ); } catch ( Exception e ) { System.out.println( e.getMessage() ); System.out.println( “Go learn about division!” ); } System.out.println( “End of program” ); 6 try block
7
Scanner keyboard = new Scanner( System.in ); int numerator = keyboard.nextInt(); int denominator = keyboard.nextInt(); double fraction; try { if ( denominator == 0 ) throw new Exception( “Exception! You can’t divide by zero”); fraction = (double) numerator / denominator ; System.out.println( fraction ); } catch ( Exception e ) { System.out.println( e.getMessage() ); System.out.println( “Go learn about division!” ); } System.out.println( “End of program” ); 7 catch block
8
try – throw - catch try { code to try possibly throw an exception of Exception_type and jump into the catch block if no exception is thrown, execute more code ignore catch block and continue program after it } catch ( Exception_type e ){ do stuff in this special case } 8
9
Other exception Classes FileNotFoundException 9
10
Streams Objects Deliver data from program to a destination – the screen or a file (output) Take data from a source – the keyboard or a file – and deliver it to the program (input) 10
11
Streams Input and output streams of data Objects of class Scanner are input streams The object System.out is an output stream 11
12
File versus Stream File – Permanent storage for an application (everything we’ve done before ‘disappears’) – Data available externally to the program (on disk, on memory stick, in the cloud) Stream – Representation of the file in the program
13
Writing to a file 1.Specify a filename 2.Attempt to open the file associate file with a stream (fails if the file does not exist) 3.Write to the stream (file) 4.Close the stream (file) 13
14
Writing to a file java.io.PrintWriter; PrintWriter outStream = new PrintWriter( String fName ); starts with an empty file (overwrite possible) outStream.print(); outStream.println(); outStream.printf(); buffering outStream.close(); close your streams!!! 14
15
Writing to a file 1.get the filename 2.open the file 3.write to the stream 4.close the stream String fName = "readme.txt"; import java.io.PrintWriter; PrintWriter oStr; oStr = new PrintWriter(fName); oStr.println("hello"); oStr.printf(“this is %s“, fName); oStr.close();
16
import java.io.PrintWriter; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( fName ); oStream.println( "hello world!" ); oStream.close(); }
17
import java.io.PrintWriter; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( fName ); oStream.println( "hello world!" ); oStream.close(); }
18
import java.io.PrintWriter; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( fName ); oStream.println( "hello world!" ); oStream.close(); }
19
import java.io.PrintWriter; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( fName ); oStream.println( "hello world!" ); oStream.close(); } What if you run it multiple times? PrintWriter opens or creates a file … cursor is at the first position in the file (OVERWRITES any prior content)
20
import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!" ); oStream.close(); } This is how files are opened with the ability to APPEND the contents. Only changed the way the stream is created
21
import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!“ ); oStream.close(); }
22
import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!“ ); oStream.close(); } What if you try to append a file that doesn’t exist? The file will be created
23
import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!“ ); oStream.close(); } What if you run it multiple times?
24
import java.io.PrintWriter; import java.io.FileOutputStream; import java.io.FileNotFoundException; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; PrintWriter oStream; oStream = new PrintWriter( new FileOutputStream( fName, true ) ); oStream.println( "hello world!" ); oStream.close(); } What if you run it multiple times? You will get a file with 3 X N lines, where N is the number of times you run the program
25
Reading from a file 1.get the filename 2.open the file 3.read from the stream 4.close the stream String fName = "readme.txt"; import java.io.File; import java.util.Scanner; Scanner iStream; iStream = new Scanner( new File(fName)); i = iStream.nextInt(); iStream.close();
26
import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt”; String line; Scanner iStream; iStream = new Scanner( new File( fName ) ); line = iStream.nextLine(); System.out.println( line ); iStream.close(); }
27
import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt”; String line; Scanner iStream; iStream = new Scanner( new File( fName ) ); line = iStream.nextLine(); System.out.println( line ); iStream.close(); }
28
import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt”; String line; Scanner iStream; iStream = new Scanner( new File( fName ) ); line = iStream.nextLine(); System.out.println( line ); iStream.close(); } What if the file doesn’t exist? Exception in thread "main" java.io.FileNotFoundException: somefile.txt (The system cannot find the file specified)
29
import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt”; String line; Scanner iStream; iStream = new Scanner( new File( fName ) ); line = iStream.nextLine(); System.out.println( line ); iStream.close(); } What if the file is empty? Exception in thread "main" java.util.NoSuchElementException: No line found
30
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Scanner; public class TextFileApp01 { public static void main( String[] args ) throws FileNotFoundException { String fName = "somefile.txt"; String line; int lineNumber = 0; Scanner iStream = new Scanner( new File( fName ) ); while( iStream.hasNextLine() ) { line = iStream.nextLine(); lineNumber++; System.out.printf( "%d: %s\n", lineNumber, line ); } iStream.close(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.