For Friday Finish reading chapter 9 WebCT quiz 17.

Slides:



Advertisements
Similar presentations
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Advertisements

Lecture 15: I/O and Parsing
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
Chapter 11.  Data is often stored in files such as a text file  We need to read that data into our program  Simplest mechanism  Scanner class  First.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Files and Streams CS 21a Chapter 11 of Horstmann.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
Chapter 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.
18 File handling1June File handling CE : Fundamental Programming Techniques.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Computer Programming Lab(5).
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
CS1101: Programming Methodology Aaron Tan.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Two Ways to Store Data in a File Text format Binary format.
Recitation 1 CS0445 Data Structures Mehmud Abliz.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.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.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
File IO Basics By Dan Fleck Coming up: Data Streams.
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
© 2004 Pearson Addison-Wesley. All rights reserved April 24, 2006 Exceptions (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING 2006.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
Week 12 – Text Files Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. 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,
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
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.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CMSC 202 Text File I/O.
Introduction to programming in java
Reading from a file A file is typically stored on your computers hard drive. In the simplest case, lets just assume it is text. For a program to use.
Week 14 - Wednesday CS 121.
Testing and Exceptions
Some File I/O CS140: Introduction to Computing 1 Savitch Chapter 9.1, 10.1, /25/13.
File Input and Output TOPICS File Input Exception Handling File Output.
For Monday Read WebCT quiz 18.
Yong Choi School of Business CSU, Bakersfield
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
CHAPTER 5 (PART 2) JAVA FILE INPUT/OUTPUT
Reading and Writing Text Files
File I/O ICS 111: Introduction to Computer Science I
For Wednesday No new reading No quiz.
Introduction to Java Brief history of Java Sample Java Program
CSC1401 Input and Output (with Files)
File Input and Output.
I/O Exceptions & Working with Files
Presentation transcript:

For Friday Finish reading chapter 9 WebCT quiz 17

Program 6 Any questions?

Handling Exceptions

Files What are files? Why are they important?

File Processing Programs What needs to be accomplished in a file processing program?

The Scanner Class

Reading Lines of text Single words Integers Doubles Etc.

Opening a File Creating the File object. The FileNotFoundException

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** Read census data, printing all those lines containing the string "AK" (Alaska). * Byron Weber Becker */ public class ReadCountyData { public static void main(String[ ] args) {// Open the file. Scanner in = null; try {File file = new File("2000US_County_data.txt"); in = new Scanner(file); } catch (FileNotFoundException ex) {System.out.println(ex.getMessage()); System.out.println("in " + System.getProperty("user.dir")); System.exit(1); }

// Read and process each record. while (in.hasNextLine()) {String record = in.nextLine(); if (record.indexOf("AK") >= 0) // records containing “AK” (Alaska) { System.out.println(record); } // Close the file. in.close(); }

Writing Files Writer classes Each classes has a purpose –FileWriter – connects to a file –BufferedWriter – makes output more efficient –PrintWriter – lets you use print and println

Using the FileWriters FileWriter fw = new FileWriter(fileName); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw); out.println(“the first line”);

Alternative PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter(fileName))); out.println(“the first line”);

Notes Using files always potentially throws IOExceptions. Sometimes we can handle these gracefully. Sometimes we need to allow the program to report an error and end.

File Practice Write code to read a file of integers and write a new file with one integer per line of the new file.

File Practice The files input1.txt and input2.txt each contain a list of numbers of type int in ascending order. Write a program to create a new file, output.txt, that contains all of the numbers from both input files in sorted order. Use a function that will accept as parameters any two open input streams and an open output stream.

Objects and Records Approaches to reading objects Approaches to writing objects Issues to deal with –New lines –Multiple word fields

The File Class Contains a number of methods Useful for answering questions about files

Interacting with the User How is this similar to files? How is it different?

System.in

Practice Problem Write Java code to ask for a student’s exam score out of 100. Your program is then to match the exam score to a letter grade and print the grade to the screen. The letter grade is to be calculated as follows: 90 and aboveA 80-89B 70-79C 60-69D below 60F

Switch Practice A program is required to read a customer’s name, a purchase amount and a tax code. The tax has been validated and will be one of the following: 0tax exempt (0%) 1state sales tax only (3%) 2federal and state sales tax (5%) 3special sales tax (7%) The program must then compute the sales tax and the total amount due and print the customer’s name, purchase amount, sales tax and total amount due.

Practice Design and write a program that will prompt for, receive, and total a collection of payroll amounts entered by the user until a sentinel amount of -99 is entered. After the sentinel has been entered, display the total payroll amount on the screen.

Problem 3 Write a program that sums a sequence of integers. Assume that the first integer read specifies the number of values remaining to be entered. Your program should read only one value at a time. A typical input sequence might be

Problem 4 Write a program that finds the smallest of several integers. Assume that input will end when a sentinel value of –999 is read. Do not count –999 as one of the integers to consider.