Building java programs chapter 6

Slides:



Advertisements
Similar presentations
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.
Advertisements

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:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Chapter 2 How to Compile and Execute a Simple Program.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Program 6 Any questions?. System.in Does the opposite of System.out.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
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
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
1 BUILDING JAVA PROGRAMS CHAPTER 7 LECTURE 7-3: ARRAYS AS PARAMETERS; FILE OUTPUT.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Topic 19 file input, line based Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
Advanced File Processing NOVEMBER 21 ST, Objectives Write text output to a file. Ensure that a file can be read before reading it in your program.
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:
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Building java programs, chapter 5 Program logic and indefinite loops.
FILE PROCESSING. Reading files To read a file, pass a File when constructing a Scanner. Scanner name = new Scanner(new File(" file name ")); Example:
Building java programs, chapter 3 Parameters, Methods and Objects.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
COP3502: Introduction to Computer Science Yashas Shankar Midterm review.
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.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 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,
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
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.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
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.
1 Sections 3.3 – 3.4 Terminal I/O and Comments Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Part I General Principles
Building Java Programs
Streams & File Input/Output (I/O)
Introduction to programming in java
Lecture 3: Input/Output
Output to files reading: 6.4.
Hours question Given a file hours.txt with the following contents:
I/O Basics.
Introduction to pseudocode
Building Java Programs Chapter 6
CIS 110: Introduction to Computer Programming
Building Java Programs
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
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
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:

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