Java IO and C++ Streams

Similar documents
Streams - Object input and output in C++

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

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

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

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

Software 1. Java I/O

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

Module 11 The C++ I/O System

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

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

Chapter 12 - C++ Stream Input/Output

CSD Univ. of Crete Fall Files, Streams, Filters

Exceptions and Working with Files

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

basic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits>

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

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

CSC 1214: Object-Oriented Programming

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

Streams. Programação Orientada por Objetos (POO) Centro de Cálculo Instituto Superior de Engenharia de Lisboa

Writing usable APIs in practice

Software 1. תרגול 9 Java I/O

CS11 Advanced C++ Lecture 2 Fall

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

System.out.format("The square root of %d is %f.%n", i, r);

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

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

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

CS 251 Intermediate Programming Java I/O Streams

Princeton University COS 333: Advanced Programming Techniques A Subset of Java

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

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

Fig: iostream class hierarchy

PIC 20A Streams and I/O

10.1 Overview 162 CHAPTER 10 CHARACTER STREAMS

I/O Streams and Standard I/O Devices (cont d.)

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

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

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

Darshan Institute of Engineering & Technology for Diploma Studies

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

Chapter 21 - C++ Stream Input/Output

Decorator Pattern. Decorator Pattern. Decorator Problems Decorator Examples Proxy Pattern. Motivation Structure Decorator vs Inheritance vs Strategy

CPS122 Lecture: Input-Output

Jan 27, C++ STL Streams. Daniel Maleike

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

I/O Streams. Object-oriented programming

CS112 Lecture: Streams

Input/Output Streams: Customizing

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Software Practice 1 - File I/O

Pieter van den Hombergh Richard van den Ham. March 13, 2018

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

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

CS2141 Software Development using C/C++ Stream I/O

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

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

Chapter 21 - C++ Stream Input/Output

Example: Copying the contents of a file

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

CS201 Solved MCQs.

Chapter 10. IO Streams

Writing usable APIs in practice

CPS122 Lecture: Input-Output

by Pearson Education, Inc. All Rights Reserved. 2

Standard I/O in C and C++

Objec&ves. Review. Standard Error Streams

ITI Introduction to Computer Science II

10/23/02 21:20:33 IO_Examples

C++ Input/Output: Streams

Advanced I/O Concepts

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

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

Chapter 11 Customizing I/O

Chapter 11 Customizing I/O

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

Jonathan Aldrich Charlie Garrod

Introduction to C++ (Extensions to C)

Files and Streams

Chapter 8: Files and Security

UEE1303(1070) S 12 Object-Oriented Programming in C++

Object-Oriented Programming in the Java language

Setting Justification

Object Oriented Programming Using C++ UNIT-3 I/O Streams

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

Intermediate Programming Methodologies in C++ CIS22B. Joe Bentley

We will exclusively use streams for input and output of data. Intro Programming in C++

Objects and streams and files CS427: Elements of Software Engineering

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

When working with files and directories, it is often

Chapter 10 Input Output Streams

1.00 Lecture 30. Sending information to a Java program

Week 3: File I/O and Formatting 3.7 Formatting Output

JAVA V Input/Output Java, winter semester

JAVA Input/Output Java, winter semester

C17: I/O Streams and File I/O

Output Streams Input Streams Filter Streams Readers and Writers Object Streams and Serialization Introduction to Sockets

Transcription:

Java IO and C++ Streams October 22, 2004 Operator Overloading in C++ - 2004-10-21 p. 1/31

Outline Java IO InputStream/OutputStream FilterInputStream/FilterOutputStream DataInputStream/DataOutputStream Reader/Writer Encodings C++ Streams Operator Overloading in C++ - 2004-10-21 p. 2/31

java.io - Intro Two main abstractions: streams of bytes InputStream abstract class is the superclass of all classes representing an input stream of bytes OutputStream abstract class is the superclass of all classes representing an output stream of bytes streams of characters Operator Overloading in C++ - 2004-10-21 p. 3/31

java.io - Intro Two main abstractions: streams of bytes streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters Writer abstract class is the superclass of all classes representing an output stream of characters Operator Overloading in C++ - 2004-10-21 p. 4/31

OutputStream must always provide at least a method that writes one byte of output methods: void close() void flush() void write(byte[] b) void write(byte[] b, int off, int len) abstract void write(int b) subclasses Operator Overloading in C++ - 2004-10-21 p. 5/31

OutputStream (cont) subclasses: ByteArrayOutputStream data is written into a byte array the buffer automatically grows as data is written to it FileOutputStream writing data to a File or to a FileDescriptor meant for writing streams of raw byte FilterOutputStream ObjectOutputStream PipedOutputStream Operator Overloading in C++ - 2004-10-21 p. 6/31

OutputStream (cont) subclasses: ByteArrayOutputStream FileOutputStream FilterOutputStream superclass of all classes that filter output streams subclasses: BufferedOutputStream, CheckedOutputStream, CipherOutputStream, DataOutputStream, DeflaterOutputStream, DigestOutputStream, PrintStream ObjectOutputStream PipedOutputStream Operator Overloading in C++ - 2004-10-21 p. 7/31

OutputStream (cont) subclasses: ByteArrayOutputStream FileOutputStream FilterOutputStream ObjectOutputStream writes primitive data types and graphs of Java objects only objects that support the java.io.serializable interface can be written to streams primitive data types can also be written PipedOutputStream can be connected to a piped input stream to create a communications pipe Operator Overloading in C++ - 2004-10-21 p. 8/31

FilterOutputStream writes data to another OutputStream two kind of subclasses: processing of the data: BufferedOutputStream CheckedOutputStream CipherOutputStream DeflaterOutputStream GZIPOutputStream ZipOutputStream DigestOutputStream provides an expanded interface: DataOutputStream PrintStream: polymorphic print and println, printf Operator Overloading in C++ - 2004-10-21 p. 9/31

DataOutputStream Extended interface provided by DataOutputStream: void writeboolean(boolean v) void writebyte(int v) void writebytes(string s) void writechar(int v) void writechars(string s) void writedouble(double v) void writefloat(float v) void writeint(int v) void writelong(long v) void writeshort(int v) void writeutf(string str) Operator Overloading in C++ - 2004-10-21 p. 10/31

OutputStream - Example FileOutputStream fileout = new FileOutputStream("myfile"); // add buffering BufferedOutputStream bufout = new BufferedOutputStream(fileOut); // add compression GZIPOutputStream gzout = new GZIPOutputStream(bufOut); // interface for binary data DataOutputStream out = new DataOutputStream(gzOut); // some writing out.writeint(n); out.writedouble(d); Operator Overloading in C++ - 2004-10-21 p. 11/31

OutputStream - What if? FileOutputStream fileout = new FileOutputStream("myfile"); BufferedOutputStream bufout = new BufferedOutputStream(fileOut); GZIPOutputStream gzout = new GZIPOutputStream(bufOut); DataOutputStream out1 = new DataOutputStream(gzOut); DataOutputStream out2 = new DataOutputStream(bufOut); out1.writeint(n); out2.writedouble(d); Operator Overloading in C++ - 2004-10-21 p. 12/31

OutputStream - What if? FileOutputStream fileout = new FileOutputStream("myfile"); BufferedOutputStream bufout = new BufferedOutputStream(fileOut); GZIPOutputStream gzout = new GZIPOutputStream(bufOut); DataOutputStream out1 = new DataOutputStream(gzOut); DataOutputStream out2 = new DataOutputStream(bufOut); out1.writeint(n); out2.writedouble(d); The output will be corrupted! Operator Overloading in C++ - 2004-10-21 p. 13/31

OutputStream - A common construction DataOutputStream out = new DataOutputStream( new GZIPOutputStream( new BufferedOutputStream( FileOutputStream("myfile")))); out.writeint(n); out.writedouble(d); Operator Overloading in C++ - 2004-10-21 p. 14/31

InputStream similar with OutputStream but in reverse subclasses: ByteArrayInputStream FileInputStream FilterInputStream ObjectInputStream PipedInputStream SequenceInputStream represents the logical concatenation of other input streams Operator Overloading in C++ - 2004-10-21 p. 15/31

The other world: characters Writer methods that a subclass must implement are: write(char[], int, int), flush(), and close() subclasses: BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, FileWriter, PipedWriter, PrintWriter, StringWriter Reader methods that a subclass must implement are read(char[], int, int) and close() subclasses: BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader What is the big difference? Operator Overloading in C++ - 2004-10-21 p. 16/31

The other world: characters Writer methods that a subclass must implement are: write(char[], int, int), flush(), and close() subclasses: BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, FileWriter, PipedWriter, PrintWriter, StringWriter Reader methods that a subclass must implement are read(char[], int, int) and close() subclasses: BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader What is the big difference? Encodings! Operator Overloading in C++ - 2004-10-21 p. 17/31

InputStreamReader/OutputStreamWriter InputStreamReader is a bridge from byte streams to character streams reads bytes and decodes them into characters using a specified charset OutputStreamWriter the opposite of InputStreamReader Operator Overloading in C++ - 2004-10-21 p. 18/31

Encodings JDKTM 5.0 Documentation Tool Docs Internationalization Tools native2ascii Supported Encodings docs/guide/intl/encoding.doc.html Operator Overloading in C++ - 2004-10-21 p. 19/31

Another Universe: C++ Streams Operator Overloading in C++ - 2004-10-21 p. 20/31

Reference The C++ Programming Language, Third Edition by Bjarne Stroustrup. Published by Addison Wesley Longman, Inc. Operator Overloading in C++ - 2004-10-21 p. 21/31

Standard Streams Several standard streams are declared in <iostream>: ostream cout // standard output stream // of char ostream cerr // standard unbuffered output // stream for error messages ostream clog // standard output stream for // error messages wostream wcout; // wide stream ver. of cout wostream wcerr; // wide stream ver. of cerr wostream wclog; // wide stream ver. of clog Operator Overloading in C++ - 2004-10-21 p. 22/31

istream Provides:» get getline read gcount putback tie Operator Overloading in C++ - 2004-10-21 p. 23/31

ostream Provides: «put write Operator Overloading in C++ - 2004-10-21 p. 24/31

State bool good() const; // next op. might succeed bool eof() const; // end of input seen bool fail() const; // next op. will fail bool bad() const; // stream is corrupted iostate rdstate() const; // get io state flags void clear(iostate f = goodbit); // set io state flags void setstate(iostate f) { clear(rdstate() f); // add f to iostate flags } operator void*() const; // nonzero if!fail() bool operator!() const { return fail(); } Operator Overloading in C++ - 2004-10-21 p. 25/31

Formating static const fmtflags skipws, // skip whitespace on input left // pad after value right, // pad before value internal, // pad between sign and value boolalpha, // use symbolic representation dec, // base 10 output (decimal) hex, // base 16 output (hexadecimal) oct, // base 8 output (octal) scientific, // d.ddddddedd fixed, // dddd.dd showbase, // prefix oct by 0 and hex by 0x showpoint, // print trailing zeros showpos, // explicit + for positive ints uppercase; // E, X rather than e, x Operator Overloading in C++ - 2004-10-21 p. 26/31

Formating (cont) // read flags fmtflags flags() const; // set flags fmtflags flags(fmtflags f); // add flags fmtflags setf(fmtflags f) { return flags(flags() f); } // clear flags void unsetf(fmtflags mask) { flags(flags()& mask); } Operator Overloading in C++ - 2004-10-21 p. 27/31

Manipulators declared in <iomanip>, namespase std Manipulators: boolalpha, noboolalpha showbase, noshowbase showpoint, noshowpoint showpos, noshowpos skipws, noskipws, ws uppercase, nouppercase internal left, right dec, hex, oct fixed, scientific Operator Overloading in C++ - 2004-10-21 p. 28/31

Manipulators (cont) Manipulators (cont): endl, ends flush resetioflags, setioflags setbase setfill setprecision setw Operator Overloading in C++ - 2004-10-21 p. 29/31

Other kinds of streams File Streams <fstream> ifstream, ofstream, fstream String Streams <sstream> istringstream, ostringstream, stringstream wistringstream, wostringstream, wstringstream Stream Buffers Operator Overloading in C++ - 2004-10-21 p. 30/31

That s all! Have a nice weekend! Operator Overloading in C++ - 2004-10-21 p. 31/31