Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.

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

Java I/O.
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.
1 Streams and Input/Output Files Part I. 2 Introduction So far we have used variables and arrays for storing data inside the programs. This approach poses.
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
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Java I/O The Cheat Sheet. Reading and Writing The basic architecture of Java IO is pluggable. The idea is to have some very simple classes do very simple.
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.
 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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Algorithm Programming I/O via Java Streams Bar-Ilan University תשס"ח by Moshe Fresko.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
13.1 Understanding Files The File class Objects can read and write to the file system Use the File class to hold information about files and directories.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java I/O Input: information brought to program from an external source
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
1 7.0 Input-output in Java : Overview Introduction: It would be quite impossible for any program to do anything useful without performing some kind of.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © Curt Hill Java I/O Flexibility and Layering.
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.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be.
L5: Input & Output COMP206, Geoff Holmes and Bernhard Pfahringer.
Java Input/Output. Reading standard input Surprisingly complicated (GUI focus) Old-fashioned way: BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
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 OOP Lecture 17 I/O and Graphics Signe Ellegård Borch Carsten Schuermann IT University Copenhagen.
Copyright(c) Systems and Computer Engineering, Carleton Univeristy, * Object-Oriented Software Development Unit 13 I/O Stream Hierarchy Case.
1 Streams Chapter Nineteen. 2 What are streams? l Input and Output for example from the keyboard (a standard input device) or the CRT (a standard output.
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.
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.
Company Input/Output Stream –. Input/Output Stream Data Program Input Stream Output Stream.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
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 Programming: Advanced Topics 1 Input/Output and Serialization.
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.
Java Programming Language (3) - Input/Output Stream –
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Java IO Exploring the java.io package and living to talk about it.
Chapter 10: I/O Streams Input Streams Output Streams
The Java IO System Different kinds of IO Different kinds of operations
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Ch14 Files and Streams OBJECTIVES
I/O Basics.
File.
Programming in Java Files and I/O Streams
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Input and Output Stream
Files and Streams in Java
14 Files and Streams.
CS 240 – Advanced Programming Concepts
David Davenport Spring 2005
Presentation transcript:

Lab 2 Data Streams

Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams

Introduction I/O Streams are commonly used in Java for communicating over networks, with files, and between applications. Almost all network communication (except UDP communication) is conducted over streams. Hence, a thorough knowledge of I/O streams is critical for network programming in Java.

Byte-level communication is represented in Java by data streams A Data Stream Provided that the data stream is constructed correctly, what goes in one end comes out the other.

Streams may be chained together, to provide additional functionality and an easier and more manageable interface. 'a' Two Data Streams

Streams are divided into two categories:  Input streams  Output streams

Certainly, you should not try to read data from an output stream or write data to an input stream.

Reading from an Input Stream Six important low-level input streams:  ByteArrayInputStream Reads bytes of data from an in-memory array  FileInputStream Reads bytes of data from a file on the local file system  PipedInputStream Reads bytes of data from a thread pipe

 StringBufferInputStream Reads bytes of data from a string  SequenceInputStream Reads bytes of data from two or more low-level streams, switching from one stream to the next when the end of the stream is reached.  System.in Reads bytes of data from the user console

Blocking I/O is used when reading from an input stream. Blocking I/O is a term applied to any form of input or output that does not immediately return from an operation. In certain situations, blocking I/O can cause performance problems. This can be alleviated by using data buffering.

Writing to an Output Stream Six important low-level output streams:  ByteArrayOutputStream Writes bytes of data to an array of bytes  FileOutputStream Writes bytes of data to a file on the local file system  PipedOutputStream Writes bytes of data to a a communications pipe, which will be connected to a PipedInputStream.

 StringBufferOutputStream Writes bytes to a string buffer  System.err Writes bytes of data to the error stream of the user console, also known as standard error.  System.in Writes bytes of data to the user console, also known as standard output.

Like input streams, data is communicated sequentially; the first byte in will be the first byte out.

Filter Streams The basic low-level streams have limited flexibility. Filter streams add additional functionality to an existing stream. For example, allowing one to read a line of text instead of reading byte by byte.

Filter streams can be connected to any other stream (low-level stream or another filter stream). Filter stream classes extend from java.io.FilterInputStream or java.io.FilterOutputStream

Examples of Filter Output Streams BufferedOutputStream  Uses I/O buffering for output to improve system performance.  Outputs to an internal buffer. Buffer contents are dumped to the output stream when it is full or flushed.

DataOutputStream  Writes primitive data types, such as an int, float, a double, or even a line of text, to an output stream. PrintStream  Offers additional methods for writing lines of text, and other datatypes as text.  Provides a convenient way to print primitive datatypes as text using the print() and println() method.

Example: FileOutputStream fout; DataOutputStream dos; fout = new FileOutputStream("out"); dos = new DataOutputStream(fout); dos.writeInt(1024); dos.writeFloat(43.235); 1024 FileOutput Stream DataOutput Stream File int bytes

Examples of Filter Input Streams BufferedInputStream  Uses I/O buffering for input to improve system performance.  Tries to reduce the number of times an application blocks for input by reading bytes in batches into a buffer.

DataInputStream  Reads primitive data types, such as an int, float, a double, or even a line of text, from an input stream.

Example: FileInputStream fin; DataInputStream dis; fin = new FileInputStream("out"); dis = new DataInputStream(fin); int intData = dis.readInt(); float floatData = dis.readFloat(); System.out.println("Int data: "+intData); System.out.println("Float data: "+floatData); 1024 FileInput Stream DataInput Stream File int bytes