{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

18 - Not the Books Approach CS133 Developing Programming...

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

View Full Document Right Arrow Icon
1 CS133: Developing Programming Principles Lecture 18 Text Files 2 CS 133 Course Notes Lecture 18, Slide 2 Not the Book’s Approach We’re doing things a little differently than the approach in the text.
Background image of page 1

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

View Full Document Right Arrow Icon
3 CS 133 Course Notes Lecture 18, Slide 3 Text files A text file contains a number of lines, each consisting of a sequence of characters. To: Carrie From: Charlie The quick brown fox jumped over the lazy dog. 4 CS 133 Course Notes Lecture 18, Slide 4 Reading a Text File 1. Open the file, using a name known to the operating system: C:\My Stuff\CS133\Junk\test.txt /u/charlie/Courses/CS133/test.txt 2. Read and process each line of text. 3. Close the file.
Background image of page 2
5 CS 133 Course Notes Lecture 18, Slide 5 Opening the File import java.io.*; import java.util.*; FileReader in = new FileReader( filename ); Scanner myScanner = new Scanner(in); 6 CS 133 Course Notes Lecture 18, Slide 6 Class FileReader FileReader is a subclass of InputStream and can be used to create a Scanner . FileReader in = new FileReader("C:\\test.txt"); Throws FileNotFoundException if the file does not exist or can’t be opened.
Background image of page 3

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

View Full Document Right Arrow Icon
7 CS 133 Course Notes Lecture 18, Slide 7 Reading and Printing a File try { FileReader in = new FileReader("C:\\test.txt"); Scanner scanner = new Scanner(in); while (scanner.hasNext()) { System.out.println(scanner.nextLine());
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}