— Scott McNealy, Sun Microsystems Chairman, President and ceo


Write the three serialized



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

Write the three serialized 
character objects to a fi le
Create a fi le and write three serialized 
character objects. The fi le won’t make 
sense if you try to read it as text:
 ̈ÌsrGameCharacter 
̈%gê8MÛIpowerLjava/lang/
String;[weaponst[Ljava/lang/
String;xp2tlfur[Ljava.lang.String;≠“VÁ
È{Gxptbowtswordtdustsq~»tTrolluq~tb
are handstbig axsq~xtMagicianuq~tspe
llstinvisibility
1
power: 200
type: Troll
weapons: bare
hands, big ax
power: 120
type: Magician
weapons: spells, 
invisibility
object
object


saving 
objects
432
 
chapter 14
Here are the steps for serializing (saving) an object. Don’t bother 
memorizing all this; we’ll go into more detail later in this chapter.
Writing a serialized object to a file
FileOutputStream fileStream = new FileOutputStream(“MyGame.ser”);
Make a FileOutputStream object. FileOutputStream 
knows how to connect to (and create) a file.
1
Make a FileOutputStream
os.writeObject(characterOne);
os.writeObject(characterTwo);
os.writeObject(characterThree);
3
Write the object
os.close();
4
Close the ObjectOutputStream
serializes the objects referenced by character-
One, characterTwo, and characterThree, and 
writes them to the file “MyGame.ser”.
Closing the stream at the top closes the ones 
underneath, so the FileOutputStream (and the 
file) will close automatically.
If the file “MyGame.ser” doesn’t 
exist, it will be created 
automatically.
ObjectOutputStream os = new ObjectOutputStream(fileStream);

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   394   395   396   397   398   399   400   401   ...   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