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

Similar documents
CS 251 Intermediate Programming Java I/O Streams

PIC 20A Streams and I/O

Algorithms. Produced by. Eamonn de Leastar

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

IT101. File Input and Output

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

Performing input and output operations using a Byte Stream

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

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

Simple Java Input/Output

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

Java Input/Output. 11 April 2013 OSU CSE 1

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

Darshan Institute of Engineering & Technology for Diploma Studies

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

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

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

C17: I/O Streams and File I/O

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

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

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

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

C17: File I/O and Exception Handling

Object-Oriented Programming Design. Topic : Streams and Files

HST 952. Computing for Biomedical Scientists Lecture 8

1.00 Lecture 30. Sending information to a Java program

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

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

Objec&ves. Review. Standard Error Streams

Example: Copying the contents of a file

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

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

Software 1. Java I/O

Programming Languages and Techniques (CIS120)

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

Lecture 11.1 I/O Streams

Exceptions and Working with Files

Programmierpraktikum

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

Programming Languages and Techniques (CIS120)

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

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

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

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

Data Structures. 03 Streams & File I/O

Files and IO, Streams. JAVA Standard Edition

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

Programming Languages and Techniques (CIS120)

File Processing in Java

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

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

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

CSC 1214: Object-Oriented Programming

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

Experiment No: Group B_4

Sri Vidya College of Engineering & Technology Question Bank

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

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

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

CS193j, Stanford Handout #26. Files and Streams

Object Oriented Software Design

Object Oriented Software Design

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

I/O Streams. Object-oriented programming

Programming Languages and Techniques (CIS120)

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

Today s plan Discuss the Bb quiz 1 Clarify Lab 1 Review basic Java materials Classes, Objects, Interfaces Strings Java IO. Understanding Board

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

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

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

Programming Languages and Techniques (CIS120)

Streams and File I/O

Streams and File I/O

Dining philosophers (cont)

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

Today s plan Discuss the Lab 1 Show Lab 2 Review basic Java materials Java API Strings Java IO

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

Chapter 10. IO Streams

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

I/O STREAM (REQUIRED IN THE FINAL)

I/O Streams in Java. Produced by: Eamonn de Leastar Dr. Siobhán Drohan

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

Java Console Input/Output The Basics

Full file at

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CS112 Lecture: Streams

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

ITI Introduction to Computer Science II

Active Learning: Streams

Software Practice 1 - File I/O

Program Elements -- Introduction

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

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

CSC Java Programming, Fall Java Data Types and Control Constructs

Contents. iii Copyright 1998 Sun Microsystems, Inc. All Rights Reserved. Enterprise Services August 1998, Revision B

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Lecture 23. Java File Handling. Dr. Martin O Connor CA166

Training topic: OCPJP (Oracle certified professional Java programmer) or SCJP (Sun certified Java programmer) Content and Objectives

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

Transcription:

Lecture 22 Java Input/Output (I/O) Streams Dr. Martin O Connor CA166 www.computing.dcu.ie/~moconnor

Topics I/O Streams Writing to a Stream Byte Streams Character Streams Line-Oriented I/O Buffered I/O Streams Scanning and Formatting 2

I/O Streams An I/O Stream represents an input source or an output destination A stream can represent many different kinds of sources and destinations, including: disk files, peripheral devices, other programs, network sockets 3

I/O Streams Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects Some streams simply pass on data; others manipulate and transform the data in useful ways No matter how they work internally, all streams present the same simple model to programs that use them 4

Reading from a Stream A stream is a sequence of data A program uses an input stream to read data from a source, one item at a time 5

Writing to a Stream A program uses an output stream to write data to a destination, one item at time The data source and data destination can be anything that holds, generates, or consumes data These includes disk files, another program, a peripheral device, or a network socket 6

Byte Streams Programs use byte streams to perform input and output of 8-bit bytes All byte stream classes are descended from the InputStream and OutputStream classes There are many byte stream classes We'll focus on the file I/O byte streams, FileInputStream and FileOutputStream Other kinds of byte streams are used in much the same way; they differ mainly in the way they are constructed 7

Byte Streams A program named CopyBytes which uses byte streams to copy a textfile one byte at a time 8

Byte Streams CopyBytes spends most of its time in a simple loop that reads the input stream and writes the output stream, one byte at a time 9

Byte Streams Notice that read() returns an int value If the input is a stream of bytes, why doesn't read() return a byte value? 10

Byte Streams Answer: Using a int as a return type allows read() to use -1 to indicate that it has reached the end of the stream 11

Common Error Trap NOTE: Closing a stream when it's no longer needed is very important CopyBytes uses a finally block to guarantee that both streams will be closed even if an error occurs. This practice helps avoid serious resource leaks 12

Byte Streams Byte Streams are a low-level I/O which in practice, you will not use often For files with character data, you will use character streams. There are also streams for more complicated data types. Thus, Byte streams should only be used for the most primitive I/O So why talk about byte streams? Because all other stream types are built on byte streams 13

Character Streams The Java platform stores character values using Unicode conventions Character stream I/O automatically translates this internal format to and from the local character set In Western locales, the local character set is usually an 8-bit superset of ASCII For most applications, I/O with character streams is no more complicated than I/O with byte streams If internationalization isn't a priority, you may use the character stream classes without paying much attention 14 to character set issues.

Using Character Streams All character stream classes are descended from the Reader and Writer classes As with byte streams, there are character stream classes that specialize in file I/O: FileReader and FileWriter The program CopyCharacters illustrates these classes. 15

Using Character Streams 16

Using Character Streams CopyCharacters is very similar to CopyBytes The most important difference is that CopyCharacters uses FileReader and FileWriter for input and output in place of FileInputStream and FileOutputStream Notice that both CopyBytes and CopyCharacters use an int variable to read to and write from However, in CopyCharacters, the int variable holds a character value in its last 16 bits; in CopyBytes, the int variable holds a byte value in its last 8 bits. Why? 17

Using Character Streams Example: Reading the characters in from a file and printing them to screen. Note: in.read() returns an int. To obtain the char value from the int, use a type cast: inputchar = (char) c; 18

Using Character Streams Character streams are often "wrappers" for byte streams The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes FileReader, for example, uses FileInputStream, while FileWriter uses FileOutputStream. Note: A FileWriter object can be instantiated from a File object (An abstract representation of file and directory pathnames) 19

Using Character Streams FileWriter has many constructors 20

Line-Oriented I/O Character I/O usually occurs in bigger units than single characters One common unit is the line: a string of characters with a line terminator at the end. A line terminator can be a carriage-return/line-feed sequence ("\r\n"), a single carriage-return ("\r"), or a single line-feed ("\n") Supporting all possible line terminators allows programs to read text files created on any of the widely used operating systems 21

Line-Oriented I/O Let's modify the CopyCharacters program to use lineoriented I/O we need to use two new classes: BufferedReader and PrintWriter The CopyLines program on the next slide invokes BufferedReader.readLine and PrintWriter.println to do input and output one line at a time 22

Line-Oriented I/O 23

Line-Oriented I/O Invoking inputstream.readline() reads in a line of text from the input file Invoking outputstream.println() writes out a line of text to the output file and appends the line terminator for the current operating system NOTE: the line terminator for the current operating system might not be the same line terminator that was used in the input file 24

Buffered I/O Streams Most of the examples we've seen so far use unbuffered I/O This means each read or write request is handled directly by the underlying OS This makes a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive compared to memory-based processing. To reduce this kind of overhead, the Java platform implements buffered I/O streams 25

Buffered I/O Streams Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full A program can convert an unbuffered stream into a buffered stream using the wrapping mechanism we've used several times now, where the unbuffered stream object is passed to the constructor for a buffered stream class 26

Buffered I/O Streams Lets modify the constructor invocations in the CopyCharacters example to use buffered I/O There are four buffered stream classes used to wrap unbuffered streams: BufferedInputStream and BufferedOutputStream create buffered byte streams BufferedReader and BufferedWriter create buffered character streams 27

Flushing Buffered I/O Streams It often makes sense to write out a buffer at critical points, without waiting for it to fill. This is known as flushing the buffer Some buffered output classes support autoflush, specified by an optional constructor argument When autoflush is enabled, certain key events cause the buffer to be flushed For example, an autoflush PrintWriter object flushes the buffer on every invocation of the println or format methods To flush a stream manually, invoke its flush method 28

Scanning and Formatting Programming I/O often involves translating to and from the neatly formatted data humans like to work with To assist you with these chores, the Java platform provides two APIs The scanner API breaks input into individual tokens associated with bits of data The formatting API assembles data into nicely formatted, human-readable form. 29

Scanning Objects of type Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type By default, a scanner uses white space to separate tokens White space characters include blanks, tabs, and line terminators. For the full list, refer to the documentation for Character.isWhitespace. The following program ScanXan reads the individual words of a textfile and prints them out, one per line 30

Scanning ScanXan.java 31

Scanning Notice that ScanXan invokes Scanner's close method when it is done with the scanner object Even though a scanner is not a stream, you need to close it to indicate that you're done with its underlying stream Input file: xanadu.txt Output to Screen 32

Scanning To use a different token separator, invoke usedelimiter(), specifying a regular expression For example, suppose you wanted the token separator to be a comma, optionally followed by white space. You would invoke Scanner also supports tokens for all of the Java language's primitive types (except for char), as well as BigInteger and BigDecimal 33

Formatting Recall: the formatting API assembles data into nicely formatted, human-readable form. Stream objects that implement formatting are instances of either PrintWriter, a character stream class, or PrintStream, a byte stream class Note: The only PrintStream objects you are likely to need are System.out and System.err. When you need to create a formatted output stream, instantiate PrintWriter, not PrintStream. Why? 34

Formatting Both PrintStream and PrintWriter implement the same set of methods for converting internal data into formatted output Two levels of formatting are provided: print and println format individual values in a standard way format formats almost any number of values based on a format string, with many options for precise formatting 35

Formatting Invoking print or println outputs a single value after converting the value using the appropriate tostring method The format method formats multiple arguments based on a format string The format string consists of static text embedded with format specifiers; With the exception of the format specifiers, the format string is unchanged in the output (an example on next slide) Format strings support many features, here we cover just a few For a complete description: visit: http://docs.oracle.com/javase/7/docs/api/java/util/formatter.html#syntax 36

Formatting An Example of a formatted string: All format specifiers begin with a % and end with a 1- or 2- character conversion that specifies the kind of formatted output being generated d formats an integer value as a decimal value f formats a floating point value as a decimal value n outputs a platform-specific line terminator 37

Formatting Some other conversions s formats any value as a string x formats an integer as a hexadecimal value tb formats an integer as a locale-specific month name Note: Except for %% and %n, all format specifiers must match an argument. If they don't, an exception is thrown 38

Formatting In addition to the conversion, a format specifier can contain several additional elements that further customize the formatted output. Here's an example, Format, that uses every possible kind of element 39

Formatting An explanation of the longer specifier is as follows: A complete description of the above is provided at: http://docs.oracle.com/javase/tutorial/essential/io/formatting.html 40

Acknowledgement The slides are based (in part) on the I/O Streams lesson of the Online Oracle Java Tutorial http://docs.oracle.com/javase/tutorial/essential/io/streams.html 41