— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet397/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   393   394   395   396   397   398   399   400   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

saving 
objects
Capture the Beat
Use serialization
Write a fi le that holds fl attened (serialized) 
objects. Then have your program read the 
serialized objects from the fi le and infl ate them 
back into living, breathing, heap-inhabiting objects.
1
If your data will be used by only the Java 
program that generated it:
These aren’t the only options, of course. You can save data in any 
format you choose. Instead of writing characters, for example, 
you can write your data as bytes. Or you can write out any kind 
of Java primitive 
as a Java primitive—there are methods to write 
ints, longs, booleans, etc. But regardless of the method you use, 
the fundamental I/O techniques are pretty much the same: 
write some data to 
something, and usually that something is either 
a fi le on disk or a stream coming from a network connection. 
Reading the data is the same process in reverse: read some data 
from either a fi le on disk or a network connection. And of course 
everything we talk about in this part is for times when you aren’t 
using an actual database.
If your data will be used by 
other programs:
Write a plain text fi le
Write a fi le, with delimiters that other programs can parse. 
For example, a tab-delimited fi le that a spreadsheet or 
database application can use.
2


serialization 
and
file I/O
you are here
4
431
Option two
Write a plain text fi le
Create a fi le and write three lines of text, 
one per character, separating the pieces 
of state with commas:
50,Elf,bow, sword,dust
200,Troll,bare hands,big ax
120,Magician,spells,invisibility
2
The serialized file is much harder for humans to read
but it’s much easier (and safer) for your program to 
restore the three objects from serialization than from 
reading in the object’s variable values that were saved 
to a text file. For example, imagine all the ways in which 
you could accidentally read back the values in the wrong 
order.! The type might become “dust” instead of “Elf”, 
while the Elf becomes a weapon... 
Imagine you 
have three game 
characters to save...
Saving State
Imagine you have a program, say, a fantasy 
adventure game, that takes more than one 
session to complete. As the game progresses, 
characters in the game become stronger, weaker, 
smarter, etc., and gather and use (and lose) 
weapons. You don’t want to start from scratch 
each time you launch the game—it took you 
forever to get your characters in top shape for 
a spectacular battle. So, you need a way to save 
the state of the characters, and a way to restore 
the state when you resume the game. And since 
you’re also the game programmer, you want the 
whole save and restore thing to be as easy (and 
foolproof) as possible.
GameCharacter
int power
String type
Weapon[] weapons
getWeapon()
useWeapon()
increasePower()
// more
ob
j
e
ct
power: 50
type: Elf
weapons: bow, 
sword, dust
Option one

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   393   394   395   396   397   398   399   400   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish