12-files-and-exceptions

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

Info icon This 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
Image of page 1

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

View Full Document Right Arrow Icon
Files Files provide stable storage between program invocations Most programs use files for input and/or output Ex. word processors, games, etc. 2
Image of page 2
Files in Java Java makes it easy to work with files We can use Scanner to read from files just as we read from standard input The File class is used to open and manipulate a data file 3
Image of page 3

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

View Full Document Right Arrow Icon
The File Class Defined in the java.io package Usage: File foo = new File(“input.txt”); 4
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
Image of page 5

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

View Full Document Right Arrow Icon
Writing to a File This is slightly more complicated than reading from a file We’ll cover the case of writing to a text file (rather than a binary file) Strategy: use PrintWriter and FileWriter to send data to the output file 6
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 flushed automatically 7
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern