Download presentation
Presentation is loading. Please wait.
Published byVerity Charles Modified over 8 years ago
1
Advanced File Processing NOVEMBER 21 ST, 2014
2
Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.
3
Writing to a file PrintStream: 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. Why do you think the above statement is true? System.out is a PrintStream!
4
Syntax for writing to a file 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.");
5
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.
6
PrintStream PrintStream name = new PrintStream(new File("file name")); ◦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.
7
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.
8
Making sure that a file can be read The File class has a method called canRead that can be used to test 2 properties of a file: ◦The file exists at the location you are trying to read from. ◦The file can be read by the program. Example: File f = new File(“myfile.txt”); if (!f.canRead()) { System.out.println (“Bad file – how could you?!”); } else { System.out.println (“Good file – you are awesome.”); }
9
Try this! 1.Describe 3 things that you like about the person to your left (or right if there is no one to your left) and write a program to output this into a file. Write each of these 3 things on separate lines in the file. 2.Now, once you have written and closed the file, read it back in your program. Ask the person you wrote about for an input from 1 to 3. Your program should be able to prompt your friend for the input. Print out the line that your friend has selected. Make sure that your check if the file can be read in your program.
10
Announcements You will have an exam on Thursday, December 4 th. Material includes everything up to and including chapter 6. We will review during part of the day on Monday, December 1 st and all of Wednesday, December 3 rd. Don’t forget that you can potentially override your first exam score! We will be starting chapter 7 on Monday. You will have a quiz on chapter 6 on Wednesday.
11
Homework Exercise: 6.19 Programming project: 6.2 – File diff tool
12
Programming Project 6.2 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 files (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). < This file has a great deal of > This file has a grate deal of ^ < be processed > bee processed ^
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.