12-files-and-exceptions

12-files-and-exceptions - File I/O and Exceptions CSE 114:...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
File I/O and Exceptions CSE 114: Computer Science I 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Files Files provide stable storage between program invocations Most programs use fles ±or input and/or output Ex. word processors, games, etc. 2
Background image of page 2
Files in Java Java makes it easy to work with fles We can use Scanner to read ±rom fles just as we read ±rom standard input The File class is used to open and manipulate a data fle 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The File Class Defned in the java.io package Usage: File ±oo = new File(“input.txt”); 4
Background image of page 4
Reading From a File try { File f = new File(“input.txt”); Scanner s = new Scanner(f); // call Scanner methods to read input } catch (IOException ioe) { } 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Writing to a File This is slightly more complicated than reading from a ±le We’ll cover the case of writing to a text ±le (rather than a binary ±le) Strategy: use PrintWriter and FileWriter to send data to the output ±le 6
Background image of page 6
PrintWriter This class provides print() and println() methods to output text When we create a PrintWriter, we specify the character/byte output stream that it should send its characters to We can also specify whether the stream should be Fushed automatically 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 23

12-files-and-exceptions - File I/O and Exceptions CSE 114:...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online