18 - Not the Book’s Approach CS133 Developing Programming Principles Lecture 18 Text Files We’re doing things a little differently than the

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Not the Book’s Approach CS133: Developing Programming Principles Lecture 18 Text Files We’re doing things a little differently than the approach in the text. CS 133 Course Notes Lecture 18, Slide 2 1 2 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. CS 133 Course Notes Lecture 18, Slide 3 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. CS 133 Course Notes Lecture 18, Slide 4 3 4 Opening the File import java.io.*; import java.util.*; FileReader in = new FileReader(filename); Scanner myScanner = new Scanner(in); CS 133 Course Notes Lecture 18, Slide 5 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. CS 133 Course Notes Lecture 18, Slide 6 5 6 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()); } scanner.close(); } catch (FileNotFoundException e) { System.exit(0); } CS 133 Course Notes Lecture 18, Slide 7 Scanner Class All methods of the Scanner class may be used to read from the file. public boolean hasNext() The hasNext() method may be used to determine when the end of the file is reached. CS 133 Course Notes Lecture 18, Slide 8 7 8 Counting Lines of Code A commonly used software engineering metric is “lines of code”. Is this a good way of measuring programmer productivity? Is it a good way of estimating effort? How many lines did you write today? CS 133 Course Notes Lecture 18, Slide 9 Counting the number of lines in a Java file int lines = 0; try { FileReader in = new FileReader("C:\\X.java"); Scanner scanner = new Scanner(in); while (scanner.hasNext()) { scanner.nextLine(); lines++; } scanner.close(); } catch (FileNotFoundException e) { } CS 133 Course Notes Lecture 18, Slide 10 9 10 Writing a Text File 1. Create and open the file, using a name known to the operating system: C:\My Stuff\CS133\Junk\test.txt /u/charlie/Courses/CS133/test.txt Creating/Opening the File import java.io.*; FileWriter out = new FileWriter(filename); PrintWriter writer = new PrintWriter(out); 2. Generate and write each line of text. 3. Close the file. CS 133 Course Notes Lecture 18, Slide 11 CS 133 Course Notes Lecture 18, Slide 12 11 12 Class FileWriter Creates and opens an empty file for writing. If the file exists, it’s overwritten. FileWriter throws an IOException if the file can’t be created for writing. Class PrintWriter PrintWriter supports all the print() and println() methods supported by System.out. (Although System.out is not itself a PrintWriter.) CS 133 Course Notes Lecture 18, Slide 14 CS 133 Course Notes Lecture 18, Slide 13 13 14 Example Write a class to maintain information about a user between executions of a program (e.g., name and age). Store the information before exit. Restore the information at the start of execution. CS 133 Course Notes Lecture 18, Slide 15 CS 133 Example import java.util.*; import java.io.*; public class User { private String name; private int age; public User() { this.name = ""; } this.age = 0; public User(String name, int age) { this.name = name; this.age = age; } Course Notes Lecture 18, Slide 16 15 16 Example public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public String getName() { return name;} public int getAge() { return age;} CS 133 Course Notes Lecture 18, Slide 17 Example – save method public boolean save(String fileName) { PrintWriter writer; try { FileWriter out = new FileWriter(fileName); writer = new PrintWriter(out); } catch (IOException e) { return false; } writer.println(name); writer.println(age); writer.close(); return true; } CS 133 Course Notes Lecture 18, Slide 18 17 18 Example – restore method public boolean restore(String fileName) { Scanner scanner; try { FileReader in = new FileReader(fileName); scanner = new Scanner(in); } catch (FileNotFoundException e) { return false; } name = scanner.nextLine(); age = scanner.nextInt(); scanner.close(); return true; } CS 133 Course Notes Lecture 18, Slide 19 Example - Saving String fileName = "C:\\userInfo.txt"; User user = new User("Sidney Crosby", 18); user.save(fileName); CS 133 Course Notes Lecture 18, Slide 20 19 20 Example – File Contents C:\userInfo.txt Sidney Crosby 18 Example - Restoring String fileName = "C:\\userInfo.txt"; User user = new User(); user.restore(fileName); CS 133 Course Notes Lecture 18, Slide 21 CS 133 Course Notes Lecture 18, Slide 22 21 22 Appending to a Text File To add text to an existing file a different FileWriter constructor must be used: public FileWriter(String fileName, boolean append); Summary – Reading from text files. – Writing to text files. – Appending to text files. If append is true, text will be written to the end of the file. CS 133 Course Notes Lecture 18, Slide 23 CS 133 Course Notes Lecture 18, Slide 24 23 24 ERROR: undefined OFFENDING COMMAND: STACK: ...
View Full Document

This note was uploaded on 07/17/2010 for the course CS 133 taught by Professor Kierstead during the Fall '07 term at Waterloo.

Page1 / 14

18 - Not the Book’s Approach CS133 Developing Programming Principles Lecture 18 Text Files We’re doing things a little differently than the

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

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