Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.

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

Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements 3.6 – Iterators – p
© 2004 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 : Exceptions Intermediate Java Programming Summer 2007.
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.
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Chapter Day 12. © 2007 Pearson Addison-Wesley. All rights reserved5-2 Agenda Day 12 Problem set corrected  1 A, 2 B’s and 1 D Starting Late and not turning.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
CS0007: Introduction to Computer Programming File IO and Recursion.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
10-1 Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and may be caught and handled.
Program 6 Any questions?. System.in Does the opposite of System.out.
Chapter 10 Exceptions. Chapter Scope The purpose of exceptions Exception messages The call stack trace The try-catch statement Exception propagation The.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Building java programs chapter 6
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Chapter 9 File Input and Output. Objectives In this chapter you will: Learn how bits, characters, records, and files fit into a data hierarchy Explore.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
© 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.
Chapter 10 Exceptions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved File I/O in java chapter.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Scanner Review Java Foundations: Introduction to Programming and Data Structures.
© 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,
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington File handeling and Scanning.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Text File Input/Output
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Introduction to programming in java
Reading from a file and Writing to a file
File handling and Scanning COMP T1
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.
Streams and File I/O.
10 Exceptions Software Solutions Lewis & Loftus java 5TH EDITION
Loop Structures.
Text File Input/Output
I/O Basics.
Outline Boolean Expressions The if Statement Comparing Data
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
Reading and Writing Text Files
File Handling in Java January 19
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Arrays October 6, 2006 ComS 207: Programming I (in Java)
File Input and Output.
I/O Exceptions & Working with Files
Chapter 11 Exceptions Java Software Solutions
Reading Input and Scanner Class
Lecture 18 File I/O CSE /5/2019.
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Presentation transcript:

Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns true if there is more data to scan  The next returns the next scanned token as a String The fact that a Scanner is an iterator is particularly helpful when reading input from a file Let us consider an example where We want to read and process a list of URLs stored in a file

URLDissector.java The program reads the URL from a file (urls.inp) And dissect them to show the various parts of the path One scanner can be setup To read each line of the input Until the end of the file is encountered Another scanner can be setup for each URL To process each part of the path

Writing Text Files Let's now examine classes that let us write data to a text file The FileWriter class represents a text output file, but with minimal support for manipulating data Therefore, we also rely on PrintStream objects which have print and println methods defined

Writing Text Files Finally, we'll use the PrintWriter class We build the class that represents the output file by combining these classes appropriately See TestData.java (page 547) TestData.java Output streams should be closed explicitly