Building java programs chapter 6 File Processing
days until the AP Computer Science test 167
Announcement Exam is moved to Thursday, December 4. In class review for exam on Wednesday, December 3.
At the end of this class, you will be able to Write text output to a file using your program
Writing to a file PrintStream: An object in the java.io package that lets you print output to a destination such as a file. Any methods you have used on System.out (such as print, println) will work on a PrintStream. Syntax: PrintStream name = new PrintStream(new File("file name")); Example: PrintStream output = new PrintStream(new File("out.txt")); output.println("Hello, file!"); output.println("This is a second line of output."); Syntax Yoda
Details about PrintStream PrintStream name = new PrintStream(new File("file name")); If the given file does not exist, it is created. If the given file already exists, it is overwritten. The output you print appears in a file, not on the console. You will have to open the file with an editor to see it. Do not open the same file for both reading (Scanner) and writing (PrintStream) at the same time. You will overwrite your input file with an empty file (0 bytes). common PrintStream bug: - declaring it in a method that gets called many times. This causes the file to be re-opened and wipes the past contents. So only the last line shows up in the file.
System.out and PrintStream The console output object, System.out, is a PrintStream. PrintStream out1 = System.out; PrintStream out2 = new PrintStream(new File("data.txt")); out1.println("Hello, console!"); // goes to console out2.println("Hello, file!"); // goes to file A reference to it can be stored in a PrintStream variable. Printing to that variable causes console output to appear. You can pass System.out to a method as a PrintStream. Allows a method to send output to the console or a file.
Programming Project 6.2 diff – utility program that finds the difference between two files Used all the time in software engineering to compare code before and after a change. > static final int ZOOM = 2; > < //parameters.setPreviewSize(1024, 768); < parameters.setPreviewSize(1024, 576); < // parameters.setPreviewSize(512, 288); < > parameters.setPreviewSize(1280, 720); < CameraConfigurationUtils.setZoom(parameters, 2.0) > CameraConfigurationUtils.setZoom(parameters, ZOOM);
Programming Project 6.2 Rubric Comments, name provided, and subjective evaluation of code (2 points) Program compiles & runs, providing some output (2 points Asks for two filenames (1 point) Opens both files (1 point) Loops through both file (2 points) Compares lines of each file (1 point) Displays different lines between files (1 point) Extra Credit Indicate the first letter that is different between in each line (2 points). Example < This file has a great deal of > This file has a grate deal of ^ < be processed > bee processed
Homework for Chapter 6 Homework Assigned on Due on Read BJP 6.1 and write notes 11/18/2014 11/19/2014 Practice It : SC 6.5, 6.9 Practice It : Ex 6.1 Read BJP 6.2 and write notes 11/26/2014 Practice It : SC 6.6, 6.16 Practice It : Ex 6.2 Read BJP 6.3 and write notes 11/20/2014 Practice It : Ex 6.4, 6.17 Read BJP 6.4 and write notes 11/21/2014 Practice It : Ex 6.19 Programming Project: 6.2 11/24/2014