PIC 20A Streams and I/O

Similar documents
CS 251 Intermediate Programming Java I/O Streams

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

CSC 1214: Object-Oriented Programming

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

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

Objec&ves. Review. Standard Error Streams

Lecture 11.1 I/O Streams

Darshan Institute of Engineering & Technology for Diploma Studies

Chapter 10. IO Streams

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

Simple Java Input/Output

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

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

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

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

Java Input/Output. 11 April 2013 OSU CSE 1

Object-Oriented Programming Design. Topic : Streams and Files

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

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

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

Programming Languages and Techniques (CIS120)

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

Exceptions and Working with Files

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

HST 952. Computing for Biomedical Scientists Lecture 8

Software Practice 1 - File I/O

IT101. File Input and Output

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

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

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

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

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

CS193j, Stanford Handout #26. Files and Streams

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.

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

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

Files and IO, Streams. JAVA Standard Edition

Software 1. Java I/O

Dining philosophers (cont)

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

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

Experiment No: Group B_4

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

I/O Streams. Object-oriented programming

Core Java Contents. Duration: 25 Hours (1 Month)

Programming Languages and Techniques (CIS120)

I/O STREAM (REQUIRED IN THE FINAL)

Performing input and output operations using a Byte Stream

File Operations in Java. File handling in java enables to read data from and write data to files

Object-Oriented Programming in the Java language

1.00 Lecture 30. Sending information to a Java program

C17: I/O Streams and File I/O

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

ITI Introduction to Computer Science II

CS Programming I: File Input / Output

Example: Copying the contents of a file

Sri Vidya College of Engineering & Technology Question Bank

Programming Languages and Techniques (CIS120)

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

Files and Streams

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

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

Principles of Software Construction: Objects, Design and Concurrency. Design Case Study: Stream I/O. toad

CS Programming I: File Input / Output

Programming Languages and Techniques (CIS120)

PIC 20A Number, Autoboxing, and Unboxing

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

Programming Languages and Techniques (CIS120)

Unit 9: Network Programming

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

Lab 2: File Input and Output

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

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. תרגול 9 Java I/O

Introduction Unit 4: Input, output and exceptions

Software 1 with Java. The java.io package. Streams. Streams. Streams. InputStreams

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

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

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

Lecture 4: Exceptions. I/O

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

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream

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

pre-emptive non pre-emptive

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Object Oriented Design with UML and Java. PART VIII: Java IO

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

Java Console Input/Output The Basics

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

The I/O Package. THE Java platform includes a number of packages that are concerned with the CHAPTER20

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

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

Streams and File I/O

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

Programmierpraktikum

Transcription:

PIC 20A Streams and I/O Ernest Ryu UCLA Mathematics Last edited: December 7, 2017

Why streams? Often, you want to do I/O without paying attention to where you are reading from or writing to. You can read from or write to a file, the console, a network connection, or a peripheral device. You must establish such I/O channels in their own specific ways. Once a connection is established you have a stream, and you can polymorphically read from or write to a stream. 2

Introductory example String s = " http :// www. math. ucla. edu /~ eryu /" + " courses / pic20a / lectures /RJ.txt "; URL url = new URL (s); InputStream in = url. openstream (); OutputStream out = System. out ; while ( true ) { int nextbyte = in. read (); if ( nextbyte == -1) break ; out. write (( char ) nextbyte ); } in. close (); out. close (); The read and write code doesn t know what kinds of streams it is using. 3

Outline Byte streams closing streams Buffered streams Character streams Scanning and formatting Byte streams 4

abstract class InputStream An Inputstream reads (from somewhere) one byte at a time. public abstract int read () throws IOException reads and return a byte of data. It returns -1 if the end of the stream is reached. public void close () throws IOException closes the stream and releases any system resources associated with the stream. Byte streams 5

InputStream hierarchy InputStream FileInputStream FilterInputStream BufferedInputStream All classes are in the package java.io Byte streams 6

abstract class OutputStream An Outputstream writes (to somewhere) one byte at a time. public abstract void write ( int b) throws writes a byte of data. IOException public void close () throws IOException closes the stream and releases any system resources associated with the stream. Byte streams 7

OutputStream hierarchy OutputStream FileOutputStream FilterOutputStream BufferedOutputStream PrintStream All classes are in the package java.io Byte streams 8

FileInputStream and FileOutputStream FileInputStream reads bytes from a file and FileOutputStream writes bytes to a file. InputStream in; OutputStream out ; in = new FileInputStream (" filename1 "); out = new FileOutputStream (" filename2 "); while ( true ) { int nextbyte = in. read (); if ( nextbyte == -1) break ; out. write (( char ) nextbyte ); } in. close (); out. close (); Byte streams 9

PrintStream In addition to the methods inherited, PrintStream has additional methods like public void println ( int x) public void println ( double x) public void println ( Object x)... PrintStream never throws an IOException. Rather, you can use public boolean checkerror () Byte streams 10

System I/O Most operating systems (Windows, MacOS, and Linux) have 3 standard streams: standard input, standard output, and standard error. You interact with them through the command line. Use the fields of class System public static InputStream in public static PrintStream out public static PrintStream err to access these standard streams. Byte streams 11

System I/O InputStream in; OutputStream out ; in = System.in; out = System. out ; while ( true ) { int nextbyte = in. read (); if ( nextbyte == -1) break ; out. write (( char ) nextbyte ); // out. print (( char ) nextbyte ); } in. close (); out. close (); Use CTRL+D or CTRL+Z, depending on your system, to send the EOF (end of file) character to the standard input stream. Byte streams 12

Outline Byte streams closing streams Buffered streams Character streams Scanning and formatting closing streams 13

Always close streams Streams often handle are scarce, finite resources. You can run out of them if you don t clean them up properly. Always close them and close them within a finally block. When you don t close streams, they are usually closed when your program terminates. For small programs, this is usually okay. However, first we make our habits, then they make us. There s a reason experienced programmers insist that you close streams and that you do so with a finally block. closing streams 14

Always close streams Here s how you properly close the streams. InputStream in = null ; OutputStream out = null ; try { in =???; out =???;... } catch (...) {... } finally {... closing streams 15

} Always close streams... if (in!= null ) { try { in. close (); } catch ( IOException ex) { System. out. println (" Stream close failed "); } } if ( out!= null ) { try { out. close (); } catch ( IOException ex) { System. out. println (" Stream close failed "); } } If you think this is too much work, I understand. closing streams 16

try-finally When you duck the exception handling, you can use try-finally to ensure the streams are closed. public void fn () throws Exception { InputStream in = null ; try { in = new FileInputStream (" filename. txt "); // do stuff with in... } finally { in. close (); } } The try-finally construct does not catch any exceptions, but is nevertheless useful. closing streams 17

Outline Byte streams closing streams Buffered streams Character streams Scanning and formatting Buffered streams 18

Buffered streams The streams we ve talked about perform unbuffered I/O. This means the bytes are read or written one byte at a time. This can be inefficient if there is a fixed cost associated with each read/write. Buffered input streams read in bulk, and stores the data in a buffer. Reading each byte retrieves the bytes from the buffer. Buffered output stream stores the individual writes into a buffer. When the buffer is full, the entire buffer is written in bulk. Buffered streams 19

Buffered streams In Java, BufferedInputStream is an InputStream, and it s created (via the constructor) with an InputStream. The same is true with BufferedOuputStream and OutputStream. BufferedInputStream and BufferedOuputStream are wrappers. A wrappers is something that uses another functionality and provides improved efficiency or convenience. Buffered streams 20

Buffered streams In this example, buffered streams provide a huge speed-up. InputStream in = null ; OutputStream out = null ; try { in = new FileInputStream (" filename1 "); in = new BufferedInputStream ( in ); out = new FileOutputStream (" filename2 "); out = new BufferedOutputStream ( out ); while ( true ) { int nextbyte = in. read (); if ( nextbyte == -1) break ; out. write (( char ) nextbyte ); } } finally { in. close (); out. close (); } Buffered streams 21

Outline Byte streams closing streams Buffered streams Character streams Scanning and formatting Character streams 22

Avoid using byte streams All streams are implicitly or explicitly built with byte streams, so it s important to understand them. However, you should only use byte streams when you actually want to input/output bytes. If you want to input/output characters, use a character stream. It s more convenient and less error-prone. To input/output primitive types and Objects, use data streams and object streams, respectively. (We won t talk about this.) Character streams 23

Readers and Writers In Java, character streams are java.io.readers and java.io.writers. Readers and Writer are built upon InputStreams and OutputStreams. InputStream is = new FileInputStream (" name1 "); OutputStream os = new FileOutputStream (" name2 "); Reader r = new InputStreamReader ( is ); Writer w = new OutputStreamWriter ( os ); // Same as // Reader r = new FileReader (" name1 "); // Writer w = new FileWriter (" name2 "); Character streams 24

BufferedReaders and BufferedWriters For efficiency sake, you want to use BufferedReaders and BufferedWriters. Reader r; Writer w;... BufferedReader br = new BufferedReader ( r); BufferedWriter bw = new BufferedWriter ( w); With BufferedReader, you can read an entire line. String line = br. readline (); Character streams 25

Character encoding and internationalization The simplest way to encode English characters is via ASCII, which uses 8 bits per character. Java, however, supports 16-bit unicode characters (which is fortunately backwards-compatible with ASCII). To represent characters from languages like Arabic, Chinese, Greek, Hebrew, Hindi, Japanese, Korean, or Russian, you need to use unicode. We won t talk about unicode because it s a mess. Internationalization is adapting a program to different languages, regional differences and technical requirements of a target locale. Just remember that Java s characters streams have great support for internationalization. Character streams 26

Outline Byte streams closing streams Buffered streams Character streams Scanning and formatting Scanning and formatting 27

Scanning and formatting I/O often involves translating between computer-readable and human-readable representations of data. Scanner translates human-readable strings into computer-readable data. format translates computer-readable data into human-readable strings. Scanning and formatting 28

Scanner java.util.scanner can produce ints, doubles, etc. from a String or an InputStream. Scanner s; s = new Scanner (" FFF "); int i = s. nextint (16 ); // read as base 16 s = new Scanner ( System.in ); double d = s. nextdouble (); // from command line Scanner offers many more useful features. Scanning and formatting 29

format PrintStream and PrintWriter have the method format. int i = 2; double r = Math. sqrt (i); PrintStream o = System. out ; o. println (" sqrt of "+i+" is "+r+"."); o. format (" sqrt of %d is %.2 f.%n",i,r); The output is sqrt of 2 is 1.4142135623730951. sqrt of 2 is 1.41. Find instructions on how to use format at https://docs.oracle.com/javase/8/docs/api/java/util/formatter.html Scanning and formatting 30