Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Lecture 11.1 I/O Streams

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120e)

Byte and Character Streams. Reading and Writing Console input and output

Sri Vidya College of Engineering & Technology Question Bank

PIC 20A Streams and I/O

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Darshan Institute of Engineering & Technology for Diploma Studies

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

Simple Java Input/Output

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Objec&ves. Review. Standard Error Streams

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Object-Oriented Programming Design. Topic : Streams and Files

CS 251 Intermediate Programming Java I/O Streams

Java Input/Output. 11 April 2013 OSU CSE 1

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Dining philosophers (cont)

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

IT101. File Input and Output

1.00 Lecture 30. Sending information to a Java program

CSC 1214: Object-Oriented Programming

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1

Chapter 10. IO Streams

Example: Copying the contents of a file

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

needs to be reliable, easy to change, retarget efficiency is secondary implemented as interpreter, with virtual machine

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Exceptions and Working with Files

Lab 2: File Input and Output

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

CSB541 Network Programming 網路程式設計. Ch.2 Streams 吳俊興國立高雄大學資訊工程學系

Programming Languages and Techniques (CIS120)

ITI Introduction to Computer Science II

10/7/15. MediaItem tostring Method. Objec,ves. Using booleans in if statements. Review. Javadoc Guidelines

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Software Practice 1 - File I/O

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

Introduction Unit 4: Input, output and exceptions

HST 952. Computing for Biomedical Scientists Lecture 8

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Object-Oriented Programming in the Java language

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

Java Programming Lecture 9

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Simple Java I/O. Part I General Principles

Input-Output and Exception Handling

C17: File I/O and Exception Handling

Input from Files. Buffered Reader

CS Programming I: File Input / Output

Lab 10: Sockets 12:00 PM, Apr 4, 2018

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

I/O Streams. Object-oriented programming

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

I/O streams. Byte Streams Character Streams InputStream ByteArrayInputStream FileInputStream FilterInputStream

Principles of Software Construction: Objects, Design and Concurrency. Exceptions, scope, static data and methods, and Generics.

CSE 143 Lecture 25. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides adapted from Marty Stepp

File I/O Array Basics For-each loop

CS Programming I: File Input / Output

Lecture 19 Programming Exceptions CSE11 Fall 2013

CSE 143 Lecture 22. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides created by Marty Stepp

CSE 143 Lecture 21. I/O Streams; Exceptions; Inheritance. read 9.3, 6.4. slides created by Marty Stepp

Programming II (CS300)

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

C17: I/O Streams and File I/O

Performing input and output operations using a Byte Stream

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Computer Science is...

Objec-ves JAR FILES. Jar files. Excep-ons. Files Streams. Ø Wrap up Ø Why Excep-ons? 9/30/16. Oct 3, 2016 Sprenkle - CSCI209 1

Programming II (CS300)

Transcription:

Programming Languages and Techniques (CIS120) Lecture 30 April 4, 2018 I/O & Histogram Demo Chapters 28

HW7: Chat Server Announcements No penalty for late submission by tomorrow (which is a HARD deadline!) HW8: SpellChecker Available today on the website and Codio Due next Tuesday, April 10 th TA applications are now available due April 12 th See Piazza for details interviews will be during reading days. The only prerequisites are a liking teaching and being able to explain things well and help students find bugs. We are explicitly interested in having a diverse group of TAs.

Finally try {... catch (Exn1 e1) {... catch (Exn2 e2) {... finally {... A finally clause of a try/catch/finally statement always gets run, regardless of whether there is no exception, a propagated exception, or a caught exception.

Using Finally Finally is often used for releasing resources that might have been held/created by the try block: public void dosomeio (String file) { FileReader r = null; try { r = new FileReader(file); // do some IO catch (FileNotFoundException e) { // handle the absent file catch (IOException e) { // handle other IO problems finally { if (r!= null) { // don t forget null check! try { r.close(); catch (IOException e) {

Using Finally class C { public void foo() { this.bar(); System.out.println("here in foo"); public void bar() { try { this.baz(); catch (Exception e) { System.out.println("caught"); finally { System.out.println("finally"); System.out.println("here in bar"); public void baz() { throw new RuntimeException(); What happens if we do (new C()).foo()? 1. Program prints only "finally" 2. Program prints "here in bar", then "here in foo", then "finally" 3. Program prints "here in baz", then "finally", then "here in bar", then "here in foo" 4. Program prints "caught", then "finally", then "here in bar", then "here in foo"

Good Style for Exceptions In Java, exceptions should be used to capture exceptional circumstances Try/catch/throw incur performance costs and complicate reasoning about the program, don t use them when better solutions exist Re-use existing exception types when they are meaningful to the situation e.g. use NoSuchElementException when implementing a container Define your own subclasses of Exception if doing so can convey useful information to possible callers that can handle the exception.

Good Style for Exceptions It is often sensible to catch one exception and re-throw a different (more meaningful) kind of exception. e.g. when implementing WordScanner (in upcoming lectures), we catch IOException and throw NoSuchElementException in the next method. Catch exceptions as near to the source of failure as makes sense i.e. where you have the information to deal with the exception Catch exceptions with as much precision as you can BAD: try { catch (Exception e) { BETTER: try { catch (IOException e) {

java.io

Poll How many of these these classes have you used before CIS 120 (all part of the Java standard library)? Scanner Reader InputStream (e.g. System.in) FileReader BufferedReader Something else from java.io?

I/O Streams The stream abstraction represents a communication channel with the outside world. can be used to read or write a potentially unbounded number of data items (unlike a list) data items are read from or written to a stream one at a time The Java I/O library uses subtyping to provide a unified view of disparate data sources and sinks.

Low-level Streams At the lowest level, a stream is a sequence of binary numbers 11000101001011101011011010101010100101.. 197 46 182 170 The simplest IO classes break up the sequence into 8-bit chunks, called bytes. Each byte corresponds to an integer in the range 0 255.

InputStream and OutputStream Abstract classes that provide basic operations for the Stream class hierarchy: int read (); // Reads the next byte of data void write (int b); // Writes the byte b to the output These operations read and write int values that represent bytes range 0-255 represents a byte value -1 represents no more data (when returned from read) java.io provides many subclasses for various sources/sinks of data: files, audio devices, strings, byte arrays, serialized objects Subclasses also provides rich functionality: encoding, buffering, formatting, filtering

Binary IO example InputStream fin = new FileInputStream(filename); int[] data = new int[width][height]; for (int i=0; i < data.length; i++) { for (int j=0; j < data[0].length; j++) { int ch = fin.read(); if (ch == -1) { fin.close(); throw new IOException("File ended early"); data[j][i] = ch; fin.close();

BufferedInputStream Reading one byte at a time can be slow! Each time a stream is read there is a fixed overhead, plus time proportional to the number of bytes read. disk -> operating system -> JVM -> program disk -> operating system -> JVM -> program disk -> operating system -> JVM -> program A BufferedInputStream presents the same interface to clients, but internally reads many bytes at once into a buffer (incurring the fixed overhead only once) disk -> operating system ->>>> JVM -> program JVM -> program JVM -> program JVM -> program

Buffering Example FileInputStream fin1 = new FileInputStream(filename); InputStream fin = new BufferedInputStream(fin1); int[] data = new int[width][height]; for (int i=0; i < data.length; i++) { for (int j=0; j < data[0].length; j++) { int ch = fin.read(); if (ch == -1) { fin.close(); throw new IOException("File ended early"); data[j][i] = ch; fin.close();

The Standard Java Streams java.lang.system provides an InputStream and two standard PrintStream objects for doing console I/O. System.in System.out System.err Note that System.in, for example, is a static member of the class System this means that the field in is associated with the class, not an instance of the class. Recall that static members in Java act like global variables.

PrintStream Methods PrintStream adds buffering and binary-conversion methods to OutputStream void println(boolean b); // write b followed by a new line void println(string s); // write s followed by a newline void println(); // write a newline to the stream void print(string s); void flush(); // write s without terminating the line (output may not appear until the stream is flushed) // actually output characters waiting to be sent Note the use of overloading: there are multiple methods called println The compiler figures out which one you mean based on the number of arguments, and/or the static type of the argument you pass in at the method s call site. The java I/O library uses overloading of constructors pervasively to make it easy to glue together the right stream processing routines

Character based IO A character stream is a sequence of 16-bit binary numbers 0000010010100011011011010101010100101.. 593 46,762 \u0251 \ub6aa a The character-based IO classes break up the sequence into 16- bit chunks, of type char. Each character corresponds to a letter (specified by a character encoding).

Reader and Writer Similar to the InputStream and OutputStream classes, including: int read (); // Reads the next character void write (int b); // Writes the char to the output These operations read and write int values that represent unicode characters read returns an integer in the range 0 to 65535 (i.e. 16 bits) value -1 represents no more data (when returned from read) requires an encoding (e.g. UTF-8 or UTF-16, set by a Locale) Like byte streams, the library provides many subclasses of Reader and Writer Subclasses also provides rich functionality. use these for portable text I/O Gotcha: System.in, System.out, System.err are byte streams So wrap in an InputStreamReader / PrintWriter if you need unicode console I/O

Design Example: Histogram.java A design exercise using java.io and the generic collection libraries (SEE COURSE NOTES FOR THE FULL STORY)

Problem Statement Write a program that, given a filename for a text file as input, calculates the frequencies (i.e. number of occurrences) of each distinct word of the file. The program should then print the frequency distribution to the console as a sequence of word: freq pairs (one per line). Histogram result: The : 1 Write : 1 a : 4 as : 2 calculates : 1 command : 1 console : 1 distinct : 1 distribution : 1 e : 1 each : 1 file : 2 filename : 1 for : 1 freq : 1 frequencies : 1 frequency : 1 given : 1 i : 1 input : 1 line : 2 number : 1 occurrences : 1 of : 4 one : 1 pairs : 1 per : 1 print : 1 program : 2 sequence : 1 should : 1 text : 1 that : 1 the : 4 then : 1 to : 1 word : 2