File-based Persistence: Serializability COMP53 Dec 7, 2007.

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

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.
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 
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
Serializable Objects Serializable objects –Directly read/write from/to disk with a single statement –Convert from/to a sequence of bytes when read/write.
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.
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
CIS 270—Application Development II Chapter 14—Files and Streams.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
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.
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.
©2007 · Georges Merx and Ronald J. NormanSlide 1 Chapter 10 Information Management in Java.
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.
Chapter 14 - Designing Data Access Classes1 Chapter 14 Designing Data Access Classes.
Object Persistence and Object serialization CSNB534 Asma Shakil.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
CIS 270—App Dev II Big Java Chapter 19 Files and Streams.
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.
 Pearson Education, Inc. All rights reserved Files and Streams.
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.
Object Serialization. Sequential-access Text Files Sequential-access files store records In order by the record-key field Java imposes no structure on.
©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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
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.
1 Copyright © 2011 Tata Consultancy Services Limited TCS Internal.
Files and Serialization. Files Used to transfer data to and from secondary storage.
CSE 1020: Exceptions and A multiclass application Mark Shtern 1-1.
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 Programming: Advanced Topics 1 Input/Output and Serialization.
XSLT in Practice. Exercises  download Apache Xalan - install it - try the example in Xalan-Java Overview  ZVON XSLT Tutorial.
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
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.
CSG2H3 Object Oriented Programming
OBJECT ORIENTED PROGRAMMING II LECTURE 21 GEORGE KOUTSOGIANNAKIS
Object Writing in files
Object Serialization in Java
Text File Input/Output
Memento Design Pattern
Chapter 17 Binary I/O 1.
Java Socket Programming and Java RMI CS
Java Serialization B.Ramamurthy 11/8/2018 B.Ramamurthy.
Client server programming
The Object-Oriented Thought Process Chapter 12
Java Serialization B.Ramamurthy 11/14/2018 B.Ramamurthy.
Working with files.
JAVA IO.
Object Serialization Explanation + Example of file + network
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
Podcast Ch23f Title: Serialization
Reading and Writing Text Files
The command invocation protocol
Chapter 8: Exceptions and I/O Streams
Java Socket Programming and Java RMI CSE 291
Presentation transcript:

File-based Persistence: Serializability COMP53 Dec 7, 2007

Persistence an object has persistence if it has a lifetime that longer than the program that creates or manipulates it Two common methods for implementing persistence – file storage: write and read data from a file – database: store objects in a database such as Oracle, SQL Server, Sybase, Access, MySQL …

Java Serializability Java provides the file-based persistence through serializability: objects are converted to byte sequences that can be read and written through I/O streams

Making Objects Serializable Serializable classes must implement the Serializable interface – this interface has no methods, it is simply a marker to tell Java that instances can be serialized – JCF classes all implement Serializable, so collections can be easily stored in files To make your own classes serializable: – import java.io.Serializable; – public class PhoneBookEntry implements Serializable { }

Writing Objects to a File Open a file as an output stream: – FileOutputStream fos = new FileOutputStream(“myobjects.ser”); Connect an object output stream to your file – ObjectOutputStream out = new ObjectOutputStream(fos); Send your object(s) to the output stream: – out.writeObject(phone_book); Close the stream: – out.close();

Writing Objects to a File - Notes A typical file name for serialized objects ends with.ser, but any filename is allowed Possible exceptions: – java.io.FileNotFoundException ( FileOutputStream contructor) – java.io.IOException (most Java input/output methods) The input/output library must be imported: – import java.io.*;

Reading Objects from a File Open a file as an input stream: – FileInputStream fis = new FileInputStream(“myobjects.ser”); Connect an object output stream to your file – ObjectInputStream in = new ObjectInputStream(fis); Get your object(s) from the output stream: – phone_book = (Map )in.readObject(); Close the stream: – in.close();

Reading Objects from a File - Notes Possible exceptions: – java.io.FileNotFoundException ( FileInputStream contructor) – java.io.IOException (most Java input/output methods) – java.lang.ClassNotFoundException ( ObjectInputStream.readObject ()) The input/output library must be imported: – import java.io.*;