1. Trang chủ
  2. » Giáo án - Bài giảng

Ref Files and Serialization.ppt

15 231 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 15
Dung lượng 104,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

A Portion of Java’s File ClassesObject InputStream OutputStream FileInputStream ObjectInputStream ObjectOutputStream FileOutputStream File These classes are included in the java.io packa

Trang 1

Files and Serialization

Nguyen Thanh Phuoc

Trang 2

Used to transfer data to and from secondary storage

Diskette Memory Input file

Output file

Trang 3

A Portion of Java’s File Classes

Object

InputStream OutputStream

FileInputStream ObjectInputStream ObjectOutputStream FileOutputStream

File

These classes are included in the java.io package,

which must be imported into any application that uses

Trang 4

Basic Algorithm for File Output

Open an output connection to a file

Write the data to the file

Close the output connection to the file

FileOutputStream

Disk

Trang 5

Basic Algorithm for File Input

Open an input connection to a file

Read the data from the file and process it Close the input connection to the file

FileInputStream

Disk

Trang 6

Files and Exceptions

• Java requires the programmer to catch file exceptions

• For example, an IOException can be thrown during

at attempt to open a file

Trang 7

Serializing a Data Model

• A data is said to be serialized when it can be

permanently saved on a storage medium

• Serialization requires file management

• Java support object serialization

Trang 8

Serializing a Data

• All classes to be serialized must implement the

Serializable interface

• This interface is defined in the java.io package

• Many standard java classes are already serializable

Trang 9

import java.io.Serializable;

public class Employee implements Serializable{

private String name;

private int[] days;

public Employee(String name, int[] days){

this.name = name;

this.days = days;

}

}

Serializing the Employee System

Trang 10

Writing Objects to Output Files

• Create a new instance of FileOutputStream

• Wrap around this object a new instance of

ObjectOutputStream

• Use the method writeObject to send an object out

on the stream

Trang 11

Writing Objects to Output Files

new FileOutputStream(<a String>)

new ObjectOutputStream(<a FileOutputStream>)

<an ObjectOutputStream>.writeObject(<any object>)

A file name

Trang 12

Writing Objects to Output Files

int[] days = {8, 8, 8, 10, 6};

Employee emp = new Employee("Bill", days);

try{

FileOutputStream fos = new

FileOutputStream("employee.dat");

ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(emp);

fos.flush();

Trang 13

Reading Objects from Input Files

• Create a new instance of FileInputStream

• Wrap around this object a new instance of

ObjectInputStream

• Use the method readObject to receive an object from

the stream

Trang 14

Reading Objects from Input Files

new FileInputStream(<a String>)

new ObjectInputStream(<a FileInputStream>)

(<class name>) <an objectInputStream>.readObject()

A file name

Trang 15

Reading Objects from Input Files

Employee emp;

try{

FileInputStream fis = new FileInputStream("employee.dat"); ObjectInputStream ois = new ObjectInputStream(fis);

emp = (Employee) ois.readObject();

fis.close();

}catch(Exception e){

System.out.println("Error in input:" + e.toString());

}

Ngày đăng: 16/07/2014, 01:00

TỪ KHÓA LIÊN QUAN

w