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.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

Java for C++ Programmers Second Night. Overview First Night –Basics –Classes and Objects Second Night –Enumerations –Exceptions –Input/Output –Templates.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Yoshi
1 Various Methods of Populating Arrays Randomly generated integers.
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.
1 User Input Create a Scanner object: Scanner inx = new Scanner(System.in); System.out.println("Type a number"); int x = inx.nextInt(); System.out.println("The.
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.
CIS 1068 Program Design and Abstraction
File I/O and Exceptions File I/O Exceptions Throwing Exceptions Try statement and catch / finally clauses Checked and unchecked exceptions Throws clause.
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:
18 File handling1June File handling CE : Fundamental Programming Techniques.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
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.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
CS0007: Introduction to Computer Programming File IO and Recursion.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
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
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
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.
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.
Week 14 - Monday.  What did we talk about last time?  Inheritance.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
For Friday Finish reading chapter 9 WebCT quiz 17.
File - CIS 1068 Program Design and Abstraction
File I/O CLI: File Input CLI: File Output GUI: File Chooser
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Introduction to programming in java
Streams and File I/O.
Building Java Programs
Week 14 - Wednesday CS 121.
Advanced Java Programming
Building Java Programs Chapter 6
Building Java Programs Chapter 6
Building Java Programs
File I/O ICS 111: Introduction to Computer Science I
Fundamentals of Data Structures
Input/output (I/O) import java.io.*;
File Handling in Java January 19
Input/output (I/O) import java.io.*;
Building Java Programs
Building Java Programs
CIS 110: Introduction to Computer Programming
Building Java Programs
Presentation transcript:

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 use files for input and output  This has the advantage of requiring much less user interaction

 Most of this is done in Java through the File class  File f = new File("myFile.txt");  This class has some useful methods:  exists() - returns true if the named file exists  getName() - returns the file's name  renameTo() - changes the name of the file  delete() - delete's the file from the disk  See the Java API for more

 None of these methods can work with the contents of the file however  For that, we need to use either a Scanner (for input) or a PrintStream (for output) Scanner input = new Scanner(new File("input.txt")); PrintStream output = new PrintStream(new File("output.txt"));

 What happens when you try to compile with the code on the previous slide?  Certain types of errors must be dealt with somehow in the program  These are considered dangerous, so Java wants to make sure you know they might occur  Many types of file errors are considered checked exceptions

 The easiest way to "deal with" these errors is to use a throws clause public static void readInput() throws FileNotFoundException {... }  This tells Java "I know something bad might happen, and I accept the consequences if so"  Any method that calls a method that throws must also throw

 Once we've hooked up the file, we can use Scanner and PrintStream as usual Scanner input = new Scanner(new File("input.txt")); while (input.hasNext()) { System.out.print(input.next() + " "); } String[] words = {"cat", "dog", "bird", "hedgehog"}; PrintStream output = new PrintStream("output.txt"); for (int i = 0; i < words.length; i++) { output.println(words[i]); }

 Important notes:  Opening an existing file for output will overwrite the file ▪ You can use the exists() method of File to check for this  Never open the same file for input and output at the same time-- everything will get erased  You can walk off the end of an input file if you're not careful-- use the has methods to look before you leap

 Files are typically written such that each line is a separate entity  Therefore, we usually read files one line at a time while(file.hasNextLine()) {... }  But each line might contain multiple tokens/values

 Remember that we can make a Scanner from a String, so we can do something like: Scanner file = new Scanner(new File("myFile.txt")); while (file.hasNextLine()) { Scanner tokens = new Scanner(file.nextLine()); while (tokens.hasNext()) {... }

 Exercise 1: Write a Java program to prompt the user for an input and output file, and copy the contents of the input file to the output file.  Exercise 2: Write a Java program to read a file (specified by the user) containing a list of names and scores, and print out each person's total. ▪ Wortzman ▪ Hawker ▪ K  Should print ▪ Wortzman 32 ▪ Hawker 30 ▪ K 21