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)

Lecture 11.1 I/O Streams

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

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

PIC 20A Streams and I/O

Objec&ves. Review. Standard Error Streams

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

CS 251 Intermediate Programming Java I/O Streams

Darshan Institute of Engineering & Technology for Diploma Studies

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

Object-Oriented Programming Design. Topic : Streams and Files

Simple Java Input/Output

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

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

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

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

Dining philosophers (cont)

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

Java Input/Output. 11 April 2013 OSU CSE 1

Sri Vidya College of Engineering & Technology Question Bank

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

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

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

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

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

CSC 1214: Object-Oriented Programming

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

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

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

Example: Copying the contents of a file

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

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

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

Chapter 10. IO Streams

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

IT101. File Input and Output

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

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

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

Exceptions and Working with Files

1.00 Lecture 30. Sending information to a Java program

Programming Languages and Techniques (CIS120)

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

Programming Languages and Techniques (CIS120)

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

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

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

Programming Languages and Techniques (CIS120e)

C17: I/O Streams and File I/O

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

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

I/O Streams. Object-oriented programming

Software Practice 1 - File I/O

Lab 2: File Input and Output

ITI Introduction to Computer Science II

Experiment No: Group B_4

Simple Java I/O. Part I General Principles

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

Question 1. (2 points) What is the difference between a stream and a file?

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

HST 952. Computing for Biomedical Scientists Lecture 8

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

CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file?

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

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

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

File I/O Array Basics For-each loop

CS Programming I: File Input / Output

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

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

C17: File I/O and Exception Handling

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

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

CS 200 File Input and Output Jim Williams, PhD

CS Programming I: File Input / Output

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms

Input from Files. Buffered Reader

Software 1. Java I/O

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Java Programming Lecture 9

Character Stream : It provides a convenient means for handling input and output of characters.

Active Learning: Streams

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices

CS193j, Stanford Handout #26. Files and Streams

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

CS Week 11. Jim Williams, PhD

Performing input and output operations using a Byte Stream

CPS122 Lecture: Input-Output

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

CIS 120 Final Exam May 3, Name (printed): Pennkey (login id):

Input-Output and Exception Handling

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

Internet Technology 2/7/2013

Transcription:

Programming Languages and Techniques (CIS120) Lecture 31 November 17, 2017 I/O & Histogram Demo Chapters 28

Announcements HW8: SpellChecker Available on the website and Codio Due next Tuesday, November 21 st Next Week is Thanksgiving Break: No lab secqons Wednesday, November 22 nd : Bonus Lecture offered ONLY at the 11:00-noon Qmeslot (everyone welcome to avend) Topic: Code is Data (fun, but not relevant to HW or exam materials)

Average: 79 Median: 82 Std. Dev.: 14.3 Max: 100 Midterm 2 Results

Good Style for ExcepQons In Java, excepqons should be used to capture excep0onal circumstances Try/catch/throw incur performance costs and complicate reasoning about the program, don t use them when bever soluqons exist Re-use exis0ng excep0on types when they are meaningful to the situaqon e.g. use NoSuchElementExcepQon when implemenqng a container Define your own subclasses of ExcepQon if doing so can convey useful informaqon to possible callers that can handle the excepqon.

Good Style for ExcepQons It is oben sensible to catch one excepqon and re-throw a different (more meaningful) kind of excepqon. e.g. when implemenqng WordScanner (in upcoming lectures), we catch IOException and throw NoSuchElementException in the next method. Catch excepqons as near to the source of failure as makes sense i.e. where you have the informaqon to deal with the excepqon Catch excepqons 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 abstracqon represents a communicaqon channel with the outside world. can be used to read or write a potenqally unbounded number of data items (unlike a list) data items are read from or wriven to a stream one at a Qme 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 operaqons 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 operaqons 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 funcqonality: encoding, buffering, formamng, 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 Qme can be slow! Each Qme a stream is read there is a fixed overhead, plus Qme proporqonal to the number of bytes read. disk -> operaqng system -> JVM -> program disk -> operaqng system -> JVM -> program disk -> operaqng 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 -> operaqng 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 sta0c member of the class System this means that the field in is associated with the class, not an instance of the class. Recall that staqc 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 terminaqng the line (output may not appear unql the stream is flushed) // actually output characters waiqng to be sent Note the use of overloading: there are mul0ple methods called println The compiler figures out which one you mean based on the number of arguments, and/ or the sta0c 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 rouqnes

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 lever (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 operaqons 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 funcqonality. 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 collecqon 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 disqnct word of the file. The program should then print the frequency distribuqon 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 disqnct : 1 distribuqon : 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

Decompose the problem Sub-problems: 1. How do we iterate through the text file, idenqfying all of the words? 2. Once we can produce a stream of words, how do we calculate their frequency? 3. Once we have calculated the frequencies, how do we print out the result? What is the interface between these components? Can we test them individually?

How to produce a stream of words? 1. How do we iterate through the text file, idenqfying all of the words? public interface Iterator<T> { // returns true if the iteration has more elements public boolean hasnext(); // returns the next element in the iteration public T next(); // Optional: removes last element returned public void remove(); } Key idea: Define a class (WordScanner) that implements this interface by reading words from a text file.

Coding: Histogram.java WordScanner.java Histogram.java