Week 14 - Monday.  What did we talk about last time?  Inheritance.

Slides:



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

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.
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.
Testing and Error Handling Intro to Java. Testing We test to try and make sure our programs work correctly and have no bugs If we have access to the code,
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.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Files and Streams CS 21a Chapter 11 of Horstmann.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Using java’s Scanner class To read from input and from a file. (horstmann ch04 and ch 17)
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
CS1101: Programming Methodology Aaron Tan.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
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:
Program 6 Any questions?. System.in Does the opposite of System.out.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
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.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
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.
Creating a GUI Class An example of class design using inheritance and interfaces.
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1.
Chapter 8-Exception Handling/ Robust Programming.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
For Friday Finish reading chapter 9 WebCT quiz 17.
Intro to Computer Science Class #8 Exceptions and I/O Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault.
CSC 211 Java I File I/O and more methods. Today’s plan Homework discussion Reading lines from files Writing lines to files All of the above, using methods.
File - CIS 1068 Program Design and Abstraction
File Input / Output.
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Exceptions Chapter 9.
Reading from a file and Writing to a file
Exceptions In this lecture:
Chapter 12 Exception Handling And Text IO
MIT AITI 2003 Lecture14 Exceptions
Streams and File I/O.
Strings and File I/O.
Week 14 - Wednesday CS 121.
File Input and Output TOPICS File Input Exception Handling File Output.
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
CSS161: Fundamentals of Computing
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Advanced Java Programming
Unit 6 Working with files. Unit 6 Working with files.
Exception Handling in Java
Web Design & Development Lecture 7
Exceptions 19-Feb-19.
Exceptions 7-Apr-19.
CSC1401 Input and Output (with Files)
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
LCC 6310 Computation as an Expressive Medium
Exceptions 5-Jul-19.
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:

Week 14 - Monday

 What did we talk about last time?  Inheritance

 In this course, you have already dealt with image and audio files, but you didn't do any direct input or output to them  It is possible to read and write individual pieces of data to a file  Files are great because they exist after the program is done  Reading and writing to a file is very similar to reading and writing to the command line (using Scanner and System.out )

 Reading from a text file is almost ridiculously easy  We use Scanner, just like reading from the command line  We just have to create a new File object that gives the file we want to read from  This code will read from some file called input.txt, as if someone were typing its contents into the command line Scanner in = new Scanner(new File("input.txt"));

 Unfortunately, if you type that into Eclipse, you'll get a red squiggle underneath the code  The problem is this: What would happen if input.txt doesn't exist?  This is an error situation, and Java uses something called exceptions to deal with errors  You can catch an exception and do something to recover from the situation

 However, the error if the file isn't there is called a FileNotFoundException, and it's a checked exception  If there is the possibility of throwing a checked exception, your code has to deal with it or else your program will not compile  Well, that's annoying: Now we have to learn how to deal with catching exceptions

 You've seen exceptions before:  NullPointerException  ArrayIndexOutOfBoundsException  etc.  These are called unchecked exceptions, because you don't have to deal with them  You usually can't deal with them: They mean that you're program is messed up

 The alternative to catching an exception is throwing it up to the next level, making it someone else's problem  Sure, your program will crash if no one deals with it, but at least your code will compile  We do this by putting a throws FileNotFoundException on the declaration of main() (or whatever method we're in) public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(new File("input.txt")); public static void main(String[] args) throws FileNotFoundException { Scanner in = new Scanner(new File("input.txt"));

 Java loves objects  If you want to write to a file, you've got to create a PrintWriter object, based on a FileOutputStream object (which takes the file name as a parameter)  Once you've got a PrintWriter, you can use it just like System.out PrintWriter out = new PrintWriter(new FileOutputStream ("output.txt"));

 Just like making a Scanner from a File, making a PrintWriter from a FileOutputStream will potentially throw a FileNotFoundException  Weird, isn't it? I mean, you don't expect to find a file when you're about to write one  Sometimes Java doesn't make sense  Anyway, adding the throws FileNotFoundException to the method declaration will still solve the problem

 Unlike the command line, you should really close files when you're done reading from them  If you forget, it's okay: Java will automatically close them when your program quits  But, for situations where you're accessing multiple files, it may be important to close them Scanner in = new Scanner(new File("input.txt")); PrintWriter out = new PrintWriter(new FileOutputStream ("output.txt")); //do stuff in.close(); out.close(); Scanner in = new Scanner(new File("input.txt")); PrintWriter out = new PrintWriter(new FileOutputStream ("output.txt")); //do stuff in.close(); out.close();

 Let's write a program that prompts the user for 1o int values and then writes them to a file called numbers.txt  Then, let's write another program that opens numbers.txt, reads all 10 numbers, sorts them, and prints them out in order

 Let's write a program that prints the first million prime numbers to a file

 Finish file I/O  Lab 14

 Keep working on Project 5