Hours question Given a file hours.txt with the following contents:

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
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 Line-based file processing reading: 6.3 self-check: #7-11 exercises: #1-4, 8-11.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
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:
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture.
Topic 20 more file processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CS305j Introduction to Computing File Processing 1 Topic 18 File Processing " We have also obtained a glimpse of another crucial idea about languages and.
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.
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
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 6 Lecture 6-3: Section Problems reading:
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 6 Lecture 6-2: Line-Based File Input reading:
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.
1 Line-based file processing suggested reading:6.3.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-3: Searching Files reading: 6.3, 6.5.
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 Line-based file processing reading: 6.3 Use this if you seem to need line based processing for some aspects of your program and token based for others.
Building Java Programs
Part I General Principles
Building Java Programs
File - CIS 1068 Program Design and Abstraction
Building Java Programs Chapter 6
Lecture 3: Input/Output
Output to files reading: 6.4.
Building Java Programs
Building Java Programs
Line-based file processing
Topic 20 more file processing
Building Java Programs
Building Java Programs Chapter 6
Building Java Programs
Building Java Programs
Building Java Programs
Introduction to Computing Using Java
Building Java Programs
Building Java Programs
Building Java Programs
Input/output (I/O) import java.io.*;
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
Files Readings: 6.1 – 6.2.
Building Java Programs
Building Java Programs
Topic 19 file input, line based
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Building Java Programs
Building Java Programs
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.
Building Java Programs
Presentation transcript:

Hours question Given a file hours.txt with the following contents: 123 Alex 12.5 8.2 7.6 4.0 456 Alina 4.2 11.6 6.3 2.5 12.0 789 Ryan 16.0 12.0 8.0 20.0 7.5 Consider the task of computing hours worked by each person: Alex (ID#123) worked 32.3 hours (8.075 hours/day) Alina (ID#456) worked 36.6 hours (7.32 hours/day Ryan (ID#789) worked 63.5 hours (12.7 hours/day)

Line-based Scanner methods Scanner input = new Scanner(new File("<filename>")); while (input.hasNextLine()) { String line = input.nextLine(); <process this line>; } Method Description nextLine() returns next entire line of input (from cursor to \n) hasNextLine() returns true if there are any more lines of input to read (always true for console input)

Consuming lines of input 23 3.14 John Smith "Hello" world 45.2 19 The Scanner reads the lines as follows: 23\t3.14 John Smith\t"Hello" world\n\t\t45.2 19\n ^ String line = input.nextLine(); String line2 = input.nextLine(); Each \n character is consumed but not returned.

Output to files 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>")); Example: PrintStream output = new PrintStream(new File("out.txt")); output.println("Hello, file!"); output.println("This is a second line of output.");

Details about PrintStream PrintStream <name> = new PrintStream(new File("<file>")); 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.

File Scanner Question Write a program called Spammer.java that asks the user for an email domain and searches a file called address_book.txt. If an email with the domain name is found, the user is prompted whether the contact should be added to the "spam list" (User input in bold) Email domain to spam? @gmail.com Would you like to spam therealsherlock@gmail.com? yes The program should output the contacts that the user selected to a file named spam_list.txt Schmerlock Schmolmes <therealsherlock@gmail.com>