Advanced File Processing NOVEMBER 21 ST, 2014. Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.

Slides:



Advertisements
Similar presentations
Mr. Wortzman.  So far, we have gotten all our input and written all our output to the console  In reality, this is somewhat uncommon  Instead, we often.
Advertisements

BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: File Output; Reference Semantics reading: , 7.1, 4.3, 3.3 self-checks:
Files and Streams CS 21a Chapter 11 of Horstmann.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Chapter 2 How to Compile and Execute a Simple Program.
The NetBeans IDE CSIS 3701: Advanced Object Oriented Programming.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Program 6 Any questions?. System.in Does the opposite of System.out.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 Hours question Given a file hours.txt with the following contents: 123 Kim Eric Stef
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Building java programs chapter 6
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
Topic 19 file input, line based Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Tuesday December 4th 1. Review Game 2. Open Fall of the Roman Empire letter project Take notes from video 3. Fall of Roman Empire Video (40)
1 Reminder: String Scanners A Scanner can be constructed to tokenize a particular String, such as one line of an input file. Scanner = new Scanner( );
File Processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from "We can only.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
© 2004 Pearson Addison-Wesley. All rights reserved December 5, 2007 I/O Exceptions & Working with Files ComS 207: Programming I (in Java) Iowa State University,
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays as Parameters; File Output reading: 7.1, 4.3, 3.3 self-checks:
CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
File Output Writing data out to a file 1. Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Learning to use a ‘For Loop’ and a ‘Variable’. Learning Objective To use a ‘For’ loop to build shapes within your program Use a variable to detect input.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Building Java Programs
File Input / Output.
Output to files reading: 6.4.
Building Java Programs
Hours question Given a file hours.txt with the following contents:
String Input ICS 111: Introduction to Computer Science I
Building Java Programs Chapter 6
Introduction to Java part 2
Building Java Programs
Python programming exercise
Building Java Programs
Building Java Programs
Building Java Programs
Hours question Given a file hours.txt with the following contents:
Building Java Programs
Building Java Programs
Building Java Programs
Topic 19 file input, line based
File output; Arrays reading: 6.4 – 6.5, 7.1
I/O Exceptions & Working with Files
Introduction to Python
Building Java Programs
A counting problem Problem: Write a method mostFrequentDigit that returns the digit value that occurs most frequently in a number. Example: The number.
Building Java Programs
Building Java Programs
Presentation transcript:

Advanced File Processing NOVEMBER 21 ST, 2014

Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.

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!

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.");

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.

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.

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.

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.”); }

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.

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.

Homework Exercise: 6.19 Programming project: 6.2 – File diff tool

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 ^