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

Chapter 7 Input Output Stream

40 389 0

Đ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

Tiêu đề Input Output Stream
Thể loại Lecture notes
Định dạng
Số trang 40
Dung lượng 527 KB

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

Nội dung

Object serialization To store an object, an array of objects, or some other complex structure: • Storing all the pieces of an object separately and reconstruct the object when that da

Trang 1

Input/Output StreamChapter 7

Trang 2

Outline

Trang 3

Input/Output streams

destination

and write information to an output stream

Trang 4

Standard I/O

 There are three standard I/O streams:

standard output – defined by System.out

standard input – defined by System.in

standard error – defined by System.err

statements

 System.in typically represents keyboard input, which

we've used many times with Scanner objects

particular window on the monitor screen

Trang 5

Exception in I/O

 FileNotFoundException is thrown if the file cannot be

located (when the FileReader constructor is executed)

IOException

• A file might not exist

• Even if the file exists, a program may not be able to find it

• The file might not contain the kind of data we expect

Trang 6

Outline

Trang 7

Writing text files

Trang 8

Writing text files

write method: allows to write a content to a file

newLine method: allows to break a line

Trang 9

FileWriter fw = new FileWriter (" Lop.txt ");

BufferedWriter bw = new BufferedWriter (fw);

bw.write(" TCTH30A - Trung cap tin hoc 30A ");

bw.write(" TCTH30B - Trung cap tin hoc 30B ");

bw.write(" CDTH7K - Cao dang tin hoc 7K ");

bw.close();

}

}

Trang 10

FileWriter fw = new FileWriter (" Lop.txt “);

BufferedWriter bw = new BufferedWriter (fw);

bw.write(" TCTH30A - Trung cap tin hoc 30A\r\n ");

bw.write(" TCTH30B - Trung cap tin hoc 30B\r\n ");

bw.write(" CDTH7K - Cao dang tin hoc 7K\r\n ");

bw.close();

Trang 11

Reading text files

Trang 12

Reading text files

which allows us to read an entire line of characters in one operation

the readLine method returns a string

the readLine method returns null when no more input is available

Trang 13

FileReader fr = new FileReader (filename);

BufferedReader br = new BufferedReader (fr);

TCTH30A - Trung cap tin hoc 30A

TCTH30B - Trung cap tin hoc 30B

CDTH7K - Cao dang tin hoc 7K

Lop.txt

catch ( FileNotFoundException exception) {

System.out.println (" The file " +

filename + " was not found ");

Trang 14

Writing - Reading text files

Trang 15

FileOutputStream fis = new FileOutputStream (“LopHoc.txt”);

OutputStreamWriter isr = new OutputStreamWriter (fis, "UTF-8");

br.write(" Trung cấp tin học 30K ");

Trang 16

FileInputStream fis = new FileInputStream (filename);

InputStreamReader isr = new InputStreamReader (fis, " UTF-8 ");

BufferedReader br = new BufferedReader (isr);

catch ( FileNotFoundException exception){

System.out.println (" The file " + filename + " was not found ");

}

catch ( IOException exception){

Trang 17

Outline

Trang 18

Object serialization

 To store an object, an array of objects, or some other

complex structure:

• Storing all the pieces of an object separately and reconstruct the

object when that data is read back in  more complex the

information, the more difficult

• Object serialization: represents an object as a sequence

of bytes that can be stored in a file or transferred to another

computer

Trang 19

Serialization - Deserialization

Serialization refers to the process of saving the

state of an object by sending it to an output stream

object back into memory

Trang 20

public final void writeObject (Object writeObject x ) throws IOException

public final Object readObject() throws readObject IOException, ClassNotFoundException

Trang 21

public final Object readObject() throws readObject Exception

public final void writeObject (Object writeObject x ) throws Exception

Trang 22

We have the CountryInfo objects that we want to save to

a file

objects have been serialized and written out to a file, after that let’s deserialize them

Trang 23

import java.io.Serializable;

public class CountryInfo implements Serializable

{

private String name, abbreviation, capitol;

private long area, population;

String result = "Name: " + name + "\n";

result += "Abbreviation: " + abbreviation + "\n";

result += "Capitol: " + capitol + "\n";

result += "Area: " + area + " square kilometers\n";

result += "Population: " + population + "\n";

result += "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^";

return result;

This class implements the Serializable interface

Trang 24

Example programs

 Write – Read a CountryInfo object

 Write – Read an array of CountryInfo objects by separately

 Write – Read an array object

 Write – Read an ArrayList

Trang 25

country = new CountryInfo (“ Viet Nam ", “ VIE ", “ Ha Noi ", 9629091L, 278058900L);

FileOutputStream file = new FileOutputStream (" countries.dat ");

// Serialize the object to a file

outStream writeObject (country);

Trang 26

CountryInfo country = new CountryInfo() ;

FileInputStream file = new FileInputStream (" countries.dat ");

ObjectInputStream inStream = new ObjectInputStream (file);

// Deserialize the objects country = ( CountryInfo ) inStream readObject ();

inStream.close();

// Print the objects System.out.println (country); Read an object CountryInfo

Trang 27

Output

Trang 28

Example programs

 Write – Read a CountryInfo object

 Write – Read an array of CountryInfo objects by separately

 Write – Read an array object

 Write – Read an ArrayList

Trang 29

CountryInfo[] countries = new CountryInfo[5];

countries[0] = new CountryInfo ("United States of America", "USA", "Washington, D.C.",

FileOutputStream file = newFileOutputStream ("countries.dat");

ObjectOutputStream outStream = new ObjectOutputStream (file);

// Serialize the objects to a filefor (int i = 0; i < countries.length; i++) outStream.writeObject (countries[i]);

outStream.close();

}

Write an object separately

Trang 30

// Deserialize the objects

countries[i] = (CountryInfo) inStream.readObject();

inStream.close();

// Print the objects

Read an object separately

Trang 31

// Deserialize the objects

countries[i] = (CountryInfo) inStream.readObject();

inStream.close();

// Print the objects

System.out.println (c);

}

Read an object separately

Trang 32

Output

Trang 33

Example programs

 Write – Read a CountryInfo object

 Write – Read an array of CountryInfo objects by separately

 Write – Read an array object

 Write – Read an ArrayList

Trang 34

CountryInfo[] countries = new CountryInfo[5];

countries[0] = new CountryInfo ("United Statesof America", "USA", "Washington, D.C.",

FileOutputStream file = newFileOutputStream ("countries.dat");

ObjectOutputStream outStream = new ObjectOutputStream (file);

// Serialize the objects to a file

outStream.writeObject (countries);

Write an array of object

Trang 35

FileInputStream file = new FileInputStream (" countries.dat ");

ObjectInputStream inStream = new ObjectInputStream (file);

// Deserialize the objects

inStream.close();

// Print the objects

for ( CountryInfo c : countries) System.out.println (c);

}

}

countries = ( CountryInfo []) inStream.readObject();

Read an array of object

Trang 36

Output

Trang 37

Example programs

 Write – Read a CountryInfo object

 Write – Read an array of CountryInfo objects by separately

 Write – Read an array object

 Write – Read an ArrayList

Trang 38

Write – Read an ArrayList

The ArrayList class also implements the

Serializable interface, so we can store an entire list

of objects in one operation

If we had stored the CountryInfo objects in an ArrayList,

we should write the entire set of objects out in one

operation

Likewise, we should then read the entire ArrayList of

CountryInfo objects from the file in one operation

 Keep in mind that the objects stored in the ArrayList must also implement the Serializable interface for this to work

Trang 39

import java.io.*;

import java.util.ArrayList;

public class WriteCountryInfo3 {

public static void main (String[] args) throws IOException

arrCountries.add (new CountryInfo ("Poland", "POL", "Warsaw", 312685L, 38633900L));

FileOutputStream file = newFileOutputStream ("countries.dat");

ObjectOutputStream outStream = new ObjectOutputStream (file);

// Serialize the object arraylist to a file

Trang 40

ArrayList<CountryInfo> arrCountries = new ArrayList<CountryInfo>();

FileInputStream file = new FileInputStream ("countries.dat");

ObjectInputStream inStream = new ObjectInputStream (file);

// Deserialize the objects

arrCountries = (ArrayList) inStream.readObject();

inStream.close();

// Print the objects

System.out.println ("DOC MOT TAP CAC DOI TUONG VAO ARRAYLIST");

System.out.println ("+++++++++++++++++++++++++++++++++++++++++++++++++");

for (Object c : arrCountries)

Read an ArrayList object

Ngày đăng: 13/05/2014, 10:42

TỪ KHÓA LIÊN QUAN

w