1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.

Slides:



Advertisements
Similar presentations
A Guide to Advanced Java Faculty:Nguyen Ngoc Tu. 2 Operating System Application #1 Application #2 Java Virtual Machine #1 Local Memory Shared Memory Threads.
Advertisements

1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
COMP201 Java Programming Topic 5: Input and Output Reading: Chapter 12.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
Files Files are used to store long term data. –Typically referred to as persistent data. –A file is a group of related records. –A list of friends addresses.
CS 206 Introduction to Computer Science II 09 / 14 / 2009 Instructor: Michael Eckmann.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
COMP201 Java Programming Topic 6: Streams and Files Reading: Chapter 12.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
CIS 068 JAVA I/O: Streams and Files. CIS 068 I/O Usual Purpose: storing data to ‘nonvolatile‘ devices, e.g. harddisk Classes provided by package java.io.
Streams and Files CMPS Overview Stream classes File objects File operations with streams Examples in C++ and Java 2.
Two Ways to Store Data in a File Text format Binary format.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text Input.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
MIT AITI 2003 Lecture 15 Streams Input and Output data from/to other sources.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
Two Ways to Store Data in a File  Text format  Binary format.
Introduction to Computation and Problem Solving Class 29: Introduction to Streams Prof. Steven R. Lerman and Dr. V. Judson Harward.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
1 CISC 370: I/O Streams James Atlas June 24, 2008.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Streams & Files. Java I/O Classes Saving data to / Loading data from files Two Choices: Binary-Formatted or Text-Formatted Data – int x = 1234; – Binary.
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Parsing Text Files F Random.
Chapter 15: Input and Output
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
Java Programming, Second Edition Chapter Sixteen File Input and Output.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
java.io supports console and file I/O
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
IO in java.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
CSG2H3 Object Oriented Programming
IO in java.
OO Design and Programming II I/O: Reading and Writing
Object Writing in files
8/30/2018 CPRG 215 Introduction to Object-Oriented Programming with Java Module 5- The java.io Package Topic 5.1 Input and Output Streams, Readers.
I/O Basics.
Streams and File I/O Chapter 14.
JAVA IO.
CS 240 – Advanced Programming Concepts
File Input and Output.
Streams and Readers The stream hierarchy is for reading bytes, the reader/writer hierarchy is for reading characters Unicode characters can be used internationally,
Presentation transcript:

1 Putting Streams to use

2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java goes overboard, gives you separate classes for selecting buffering, lookahead, random access, text formatting, zip format, or binary data. 4 abstract classes at base: InputStream, OutputStream, Reader and Writer.

3 Stream Zoo InputStream and OutputStream deal with bytes DataInputStream and DataOutputStream deal with basic java types. Binary data I/O DataInputStreams Read binary data from InputStream Methods read, readByte, readChar, readDouble... DataOutputStreams Write binary data to OutputStream Methods write, writeChar, writeInt...

4 Stream Zoo File processing Import java.io Definitions of stream classes FileInputStream and FileOutputStream Inherit from InputStream and OutputStream abstract classes FileInputStream and FileOutputStream give you streams attached to disk files. FileInputStream fin = new FileInputStream (“file.dat”) Only support at byte level

5 Problem FileInputStream has no methods to read numeric types DataInputStream has no methods to get data from a file Java has creative mechanism to combine two into filtered streams by feeding existing stream to the constructor of another stream.

6 Example FileInputStream fin = new FileInputStream (“file.dat”); DataInputStream din = new DataInputStream (fin); Double s = din.readDouble ( ); The newly created filtered stream still accesses data from the file attached to the file input stream, but it now has more capable interface. NOT BUFFERED

7 Example 2 Not buffered: every call to read contacts OS to ask it to dole out another byte. IF you want buffering, data input for a file, you combine 3 types. DataInputStream din = new DataInputStream (new BufferedInputStream (new FileInputStream (“file.dat”))); Note that DataInputStream is last because we want to use DataInputStream methods (and we want them to use buffered read method).

8 Files and Streams Buffering Improves I/O performance I/O is slow compared to processor Buffer stores data of many I/O operations When full, sends data Can be explicitly flushed (forced to send data)

9 Comparisons Other languages offer niceties such as buffering and lookahead automatically in stream libraries Java’s ability to combine provides greater flexibility

10 Text Streams Set of stream filters that bridges gap between Unicode-encoded text and character encoding used by local OS. Use classes that descend from Reader and Writer (similar methods to InputStream and OutputStream) FileReader and FileWriter attach a reader or writer to a file.

11 Text Streams For text output, use PrintWriter. Can print strings and numbers in text format Must be combined with destination writer PrintWriter out = new PrintWriter (new FileWriter (“file.out”)); String name = “Harry Hacker”; double salary = ; out.print (name); out.print (‘ ‘); out.println (salary); //don’t throw exceptions

12 Text Streams No analog to read data in text format. Only possibility for processing text input is BufferedReader BufferedReader in = new BufferedReader (new FileReader (“rocks.dat”)); readLine method reads a line of text. Returns null when no more input available String line; while ((line = in.readLine () ) != null) { … }

13 Text Streams To read numbers from text input, you need to read a string first, and then convert it. String s = in.readLine ( ); Double x = Double.parseDouble (s); If more than one number on a line, you must break up the input string Use StringTokenizer utility class.

14 Text Streams To read numbers from text input, you need to read a string first, and then convert it. String s = in.readLine ( ); Double x = Double.parseDouble (s); If more than one number on a line, you must break up the input string Use StringTokenizer utility class.

15 String Tokenizers and Delimited Text Must specify delimiters to separate out tokens StringTokenizer t = new StringTokenizer (line, “|”); StringTokenizer t = new StringTokenizer (line, “ \t\n\r”); //white space StringTokenizer t = new StringTokenizer (line); //default is white space

16 Reading Delimited Input bufferedReader in = new BufferedReader (new FileReader (“file.dat”)); String s = in.readLine ( ); StringTokenizer t = new StringTokenizer (s); String name = t.nextToken (); double Salary = Double.parseDouble (t.nextToken()); int year = Integer.parseInt (t.nextToken());

17 Putting it Together Let’s see a lot of concepts together!!! - File input - Array of objects - Exception handling - Array Lists - EOF detection - Example DataTestFile.java