Object Input/Output. Object I/O  To write objects to a file, we use ObjectOutputStream  To read objects from a file we use ObjectInputStream  Lets.

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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 12 File Input and Output Animated Version.
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
Android og persistens
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.
Chapter - 12 File and Streams (continued) This chapter includes -  DataOutputStream  DataInputStream  Object Serialization  Serializing Objects 
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
File input and output. Interactive vs. file I/O All of the programs we have seen or written thus far have assumed interaction with a user, who types in.
King Saud University College of Computer and Information Sciences Department of Computer Science Dr. S. HAMMAMI Chapter 3 File Input/Output Chapter 3 File.
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--WuChapter Chapter 11 File Input and Output.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
Introduction to Input and Output. Layers (or Tiers) of an Application  Software in the real world normally takes the form of a number of independent.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12 File Input and Output.
Unit 211 File IO Binary Files Reading and Writing Binary Files Writing Objects to files Reading Objects from files.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
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.
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.
Chapter 20 – Streams and Binary Input/Output Big Java Early Objects by Cay Horstmann Copyright © 2014 by John Wiley & Sons. All rights reserved.
Files and Streams (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
More on Inheritance Dr. Andrew Wallace PhD BEng(hons) EurIng
CIS 270—Application Development II Chapter 14—Files and Streams.
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.
SE-1021 Software Engineering II Week 9, Class 3 Get ready for PollEverywhere! BlocksMania and File I/O Reading/Writing text in ASCII, Unicode, and UTF-8.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Java I/O Writing and Reading Objects to File Serialization.
Working with files. RHS – SOC 2 Motivation All our programs so far have only worked with data stored in primary storage (RAM) Data is lost when program.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Streams and File I/O.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
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.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
 Pearson Education, Inc. All rights reserved Files and Streams.
Object Serialization. Sequential-access Text Files Sequential-access files store records In order by the record-key field Java imposes no structure on.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 20.
Networking with JavaN-1 Outline Client-Server Example Steps required on the server side Steps required on the client side.
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.
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.
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
Recitation File I/O. File I/O File inFile = new File("data.txt"); File inFile = new File (“/Users/sunil/test.txt");
Streams. RHS – SWC 2 Binary files A binary file contains ”raw” bytes that can be interpreted as almost anything Can have any extension Can be created.
Operating Systems {week 11a}
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Introduction to OOP with Java 4th Ed, C. Thomas Wu
CS1101: Programming Methodology
Memento Design Pattern
Chapter 17 Binary I/O 1.
Accessing Files in Java
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Working with files.
Chapter 12 File Input and Output
Serialization and Deserialization Bullet points from Head First Java, Ch Dec-18 serialization.ppt.
Computer Science and Engineering
CSE 331 Memento Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
OBJECT ORIENTED PROGRAMMING II LECTURE 22 GEORGE KOUTSOGIANNAKIS
Java Basics Introduction to Streams.
Presentation transcript:

Object Input/Output

Object I/O  To write objects to a file, we use ObjectOutputStream  To read objects from a file we use ObjectInputStream  Lets see how we write Person objects to a file  First we need to modify the definition of the Person class in order to perform object I/O

Object I/O  If you want to perform an object I/O, then the class definition must include the phrase implements Serializable import java.io.*; class person implements Serializable { // the rest is the same // the rest is the same}

Object I/O  If a class is a subclass, then the class definition must also change.

Object I/O  You need File, FileInputStream, ObjectInputStream FileOutputStream, and ObjectOutputStream objects to do object I/O.  To write a Person object to a file, we first create an ObjectOutputStream object

Object I/O File outFile = new File ("objects. txt"); FileOutputStream outFileStream = new FileOutputStream (outFile); ObjectOutputStream outObjectStream = new ObjectOutputStream (outFileStream); To save a Person object, we write Person person = new Person ("mR. eXPRESSO", 20, 'm'); OutObjectStream.writeObject (person);

It is possible to save different types of objects to a single file. Assuming the Account and Bank classes are defined properly, we can save both types of objects to a single file: Account account 1, account 2; Bank bank1, bank2; account1 = new Account(); account2 = new Account(); bank 1 = new Bank(); bank2 = new Bank(); outObjectStream.writeObject (account 1); outObjectStream.writeObject (account 2); outObjectStream.writeObject (bank 1); outObjectStream.writeObject (bank 2);

You can even mix objects and primitives! outObjectStream.writeObject (account 1); outObjectStream.writeInt (15);

Object I/O  To read objects from a file, we use FileInputStream and ObjectInputStream  We can use the method readObject to read an object.  Since we can store any type of objects to a single file, we need to cast the object read from the file. Heres an example of reading a person object we saved in the file objects.data

Reading Objects File inFile = new File("objects.txt"); FileInputStream inFileStream = new FileInputStream (inFile); ObjectInputStream inObjectStream = new ObjectInputStream (inFileStream); Person person = (Person) inObjectStream.readObject();

See Handout Examples 1 and 2