Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

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.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
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.
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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
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.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Lecture 7 File I/O (and a little bit about exceptions)‏
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,
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Java I/O Input: information brought to program from an external source
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
Lecture 7 Exceptions and I/O. Overview of the exception hierarchy A simplified diagram of the exception hierarchy in Java Throwable ErrorException IOException.
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.
CIS 068 JAVA I/O: Streams and Files. CIS 068 I/O Usual Purpose: storing data to ‘nonvolatile‘ devices, e.g. harddisk Classes provided by package java.io.
CIS 270—Application Development II Chapter 14—Files and Streams.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Stream: an object that either delivers data to its destination (screen, file, etc.) or that takes data from a source (keyboard, file, etc.) –it acts as.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
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.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
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.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Streams and File I/O Chapter 9. Outline Overview of Streams and File I/O Text-File I/O Using the File Class Basic Binary-File I/O Object I/O with Object.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
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.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
OO Design and Programming II I/O: Reading and Writing
I/O Basics.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
I/O Streams- Basics Byte Streams and Character Streams
Streams and File I/O Chapter 14.
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Workshop for Programming And Systems Management Teachers
Workshop for Programming And Systems Management Teachers
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Files and Streams in Java
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
David Davenport Spring 2005
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:

Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng

Overview Problems Handling problems Input / output Streams

Problems What can go wrong? Boundaries Problems User inputs Memory limits Resource limits Impossible maths IO errors

Problems User inputs public class ParseException extends Exception public class DataFormatException extends Exception

Problems Memory limits public class ArrayIndexOutOfBoundsException public class NullPointerException public class OutOfMemoryError

Problems Resource limits public class FileSystemNotFoundException Public class RunTimeErrorException

Problems Impossible maths public class ArithmeticException

Problems IO errors public class FileNotFoundException public UnknownHostException

Quiz Name five problems that can occur in any java program How does Java handle the problems?

Handling problems Two types of problem handling classes Checked Anticipate and recover Try / catch block Method throws Unchecked No special handling Errors in the machine or run time errors *

Handling problems Object Throwable ErrorException RuntimeException unchecked checked

Handling problems Create your own exceptions Create a subclass of one of the Java defined classes public class EndOfTheWorldException extends Exception Add information on the error Parameter to the constructor Use the “throw” key word to throw your exception if(bError) { throw new EndOfTheWorldException(); } public void func() throws Exception

Handling problems Example try { … } catch(IOException e) { … } catch(Exception e) { … } finally { … }

Handling problems Try with resources BufferedReader br = new BufferedReader(new FileReader(path)); try { try (BufferedReader br = new BufferedReader(new FileReader(path))) { Throwable.getSuppressed

Quiz What does checked and uncheck mean in regards to java exceptions? How do you create you own exceptions?

Input / Output Communicate with the outside world Java allows many ways to get data in a send data out GUI (graphic inputs / outputs) Keyboard / screen Database input / output File input / output

Input / Output

File IO is an abstraction “File” is the data Place to read and write to is a “stream”

Input / Output Java IO package Standard streams System.in- keyboard- InputStream System.out- console- PrintStream System.err- console- PrintStream Redirect System.setIn (Out) (Err) *

Input / Output Java InputStream is abstract System.in is a specific implementation Read strings Read numbers Reads in a whole line Use Scanner to break up the string

Input / Output Scanner class Breaks up a sting into “tokens” Uses a delimiter (like space, for example) Use next functions to get data out Use has functions to check for more tokens

Input / Output Scanner sc = new Scanner(System.in); String strMsg; while(sc.hasNext()) { strMsg = sc.next(); … }

Input / Output File Save data Exists between program runs Text file Simple and readable with other programs

Input / Output The File class Represents information on the file Path Director File attributes (hidden, is it readable? Does it exists?) System independent

Quiz What is a stream in java IO? What is a file in java IO?

Input / Output Class Reader and Class Writer Java super classes Read and write text (char) Open a file Read or write to a file Close a file

Input / Output try { FileReader fInput = new FileReader(“inputfile.txt”); FileWriter fOutput = new FileWriter(“outputfile.txt); nChar = fInput.read(); fOutput.write(nChar); } catch( …) { … } finally { if(fInput != null) fInput.close(); if(fOutput != null) fOutput.close(); }

Input / Output Open a file File object FileDescriptor String (file name) FileWriter allow you to append text as well Close

Input / Output Read char as int Read an array of char Write a char as int Write an array of char Write a string flush *

Input / Output More advanced IO Connect writer to another class PrintWriter(new FileWriter(“file.txt”)); println methods for all basic java objects No exceptions - checkError()

Input / Output BufferedReader new BufferedREader(new FileReader(“file.txt”)) String readLine(); Scanner Scanner fIn = new Scanner(new File(“file.txt”)); Scanner fIn = new Scanner(new FileReader(“file.txt”));

Streams Character Input Character Output Byte Output Byte Input Byte Output Data streams Processing streams

Streams Data Streams character Unicode Reader FileReader int read methods Char arrays

Streams Data Streams Byte 8 bit data InputStream FileInputStream int read() Byte arrays

Streams Data Streams Character Writer FileWriter Byte OutputStream FileOutputStream int write void write

Streams Processing Streams Character BufferedReader BufferedWriter InputStreamReader LineNumberReader Byte ObjectInputStream (output) ZipInputStream (output)

Quiz What are the two types of steams in java? What are the basic data types used?

Questions?