Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 

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

Chapter 19 Binary I/O.
The Package Statement Group related interfaces and classes together Purpose: encapsulation and reduces name conflicts –private package classes not visible.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Streams input, output, FILE I/O, tokenization, serialization and compression.
CS-2852 Data Structures LECTURE 1B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
Lecture 7 File I/O (and a little bit about exceptions)‏
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Exception examples. import java.io.*; import java.util.*; class IO { private String line; private StringTokenizer tokenizer; public void newline(DataInputStream.
File-based Persistence: Serializability COMP53 Dec 7, 2007.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Unit 201 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Advanced Java Class Serialization. Serialization – what and why? What? –Translating the contents of an Object to a series of bytes that represent it,
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L08 (Chapter 18) Binary I/O.
Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
Working with files By the end of this lecture you should be able to: explain the principles of input and output and identify a number of different input.
1 TCSS 360, Spring 2005 Lecture Notes Design Patterns: Singleton, Memento, Flyweight.
Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Text Input and Output on the Console.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java I/O Writing and Reading Objects to File Serialization.
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,
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.
Object Persistence and Object serialization CSNB534 Asma Shakil.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Presented by Bartosz Sakowicz Java Input/Output (I/O)
Two Ways to Store Data in a File  Text format  Binary format.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
Streams and File I/O Chapter 9. Outline Overview of Streams and File I/O Text-File I/O Using the File Class Basic Binary-File I/O Object I/O with Object.
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.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
Chapter 11 Exceptions and Input/Output Operations.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 13 Java Fundamentals File I/O Serializing an.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Files and Serialization. Files Used to transfer data to and from secondary storage.
File Input & Output1 Streams & File I/O. Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Sequential files creation & writing
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Chapter 17 Binary I/O.
Object Writing in files
Object Serialization in Java
Chapter 17 Binary I/O 1.
Accessing Files in Java
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Client server programming
CS 116 Object Oriented Programming II
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Podcast Ch23f Title: Serialization
Java Basics Introduction to Streams.
Podcast Ch23c Title: Binary Files
Presentation transcript:

Chapter - 12 File and Streams (continued)

This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects  How to Write To an ObjectOutputStream  How to Read From an ObjectInputStream  Providing Object Serialization For Your Classes  Implementing The Serializable Interface

 What is a DataOutputStream ?  It is a ByteStream  It can Read Different Datatypes.  How to Open a DataOutputStream  First Open a OutputStream such as FileOutputStream to the Source  Then open a DataOutputStream with the OutputStream.  How to Read from DataOutputStream  DataOutputStream has writeXXX methods to read different data types.  Solve the problem  Let us store some tabular data in invoice1.txt. The tabular data is formatted in columns, where each column is separated from the next by tabs. The columns DataOuputStream

contain the sales price, the number of units ordered, and a description of the item,like this: Java T-shirt Java Mug  import java.io.*; public class DataIOTest { public static void main(String[] args) throws IOException { // write the data out DataOutputStream out = new DataOutputStream(new FileOutputStream("invoice1.txt")); double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 }; int[] units = { 12, 8, 13, 29, 50 };

String[ ] descs = { "Java T-shirt", "Java Mug", "Duke Juggling Dolls", "Java Pin", "Java Key Chain" }; for (int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); out.writeChar('\t'); out.writeInt(units[i]); out.writeChar('\t'); out.writeChars(descs[i]); out.writeChar('\n'); } out.close(); }

 What is a DataInputStream ?  It is a ByteStream  It can Write Different Datatypes.  How to Open a DataInputStream  First Open a InputStream such as FileInputStream to the Source  Then open a DataOutputStream with the OutputStream.  How to Read from DataOutputStream  DataOutputStream has readXXX methods to read different data types.  Solve the problem  Let us now read the invoice1.txt from previous example to read the dataitems written by previous to display a statement summarizing the order and the total amount owed. DataInputStream

import java.io.*; public class DataIOTest2 { public static void main(String[] args) throws IOException { DataInputStream in = new DataInputStream(new FileInputStream("invoice1.txt")); double price; int unit; String desc; double total = 0.0; try { while (true) { price = in.readDouble(); in.readChar(); // throws out the tab unit = in.readInt(); in.readChar(); // throws out the tab desc = in.readLine();

System.out.println("You've ordered " + unit + " units of " + desc + " at $" + price); total = total + unit * price; } } catch (EOFException e) { } System.out.println("For a TOTAL of: $" + total); in.close(); } Object Serialization  Can we read or write an object at a time ?  Yes, Two streams in java.io-- ObjectInputStream and ObjectOutputStream provide this facility.

 The key to writing an object is to represent its state in a serialized form sufficient to reconstruct the object as it is read.  Thus reading and writing objects is a process called object serialization. Serializing Objects  Reconstructing an object from a stream requires that the object first be written to a stream. So let's start there How to write to an ObjectOutputStream  Step 1  Open a OutputStream such as FileOutputStream to the Source  Step 2  Then open a ObjectOutputStream on that OutputStream.  How to write object to ObjectOutputStream  ObjectOutputStream has writeObject (Object o) method which can write supplied object.  Finally close the ObjectOutputStream with close() method.

How to read From a ObjectInputStream  Step 1  Open an InputStream such as FileInputStream to the Source  Step 2  Then open a ObjectInputStream on that InputStream.  How to read object from ObjectInputStream  ObjectInputStream has readObject method which reads an object and returns that.  Finally close the ObjectOutputStream with close() method.

 How to make an object Serializable ?  An object is serializable only if its class implements the Serializable interface.  Thus, if you want to serialize the instances of one of your classes, the class must implement the Serializable interface.  Good News about Serializable Interface!  The good news is that Serializable is an empty interface. That is, it doesn't contain any method declarations.  What is the purpose of an empty Interface  it's purpose is simply to identify classes whose objects are serializable. Providing Object Serialization for Your Classes

 Making instances of your classes serializable is easy.  You just add the implements Serializable clause to your class declaration like this: public class MySerializableClass implements Serializable {... } Implementing the Serializable Interface