— Scott McNealy, Sun Microsystems Chairman, President and ceo


objectOutputStream.writeObject(myBox)



Download 40,57 Mb.
Pdf ko'rish
bet403/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   399   400   401   402   403   404   405   406   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

objectOutputStream.writeObject(myBox);
Whatever goes here MUST implement 
Serializable or it will fail at runtime.
import java.io.*;
public class Box implements Serializable {

private int width;
private int height;
public void setWidth(int w) {
width = w;
}
public void setHeight(int h) {
height = h;
}
public static void main (String[] args) {
Box myBox = new Box();
myBox.setWidth(50);
myBox.setHeight(20);
try {
FileOutputStream fs = new FileOutputStream(“foo.ser”);
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(myBox);
os.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
Serializable is in the java.io package, so 
you need the import.
No methods to implement, but when you say 
“implements Serializable”, it says to the JVM, 
“it’s OK to serialize objects of this type.”
I/O operations can throw exceptions.
these two values will be saved
Connect to a file named “foo.ser” 
if it exists. If it doesn’t, make a 
new file named “foo.ser”.
Make an ObjectOutputStream 
chained to the connection stream.
Tell it to write the object.


438
 
chapter 14
serialized 
objects
Serialization is all or nothing.
Can you imagine what would 
happen if some of the object’s 
state didn’t save correctly?
Either the entire 
object graph is 
serialized correctly 
or serialization fails.
You can’t serialize 
a Pond object if 
its Duck instance 
variable refuses to 
be serialized (by 
not implementing 
Serializable). 
Eeewww! That 
creeps me out just thinking 
about it! Like, what if a Dog comes 
back with no weight. Or no ears. Or 
the collar comes back size 3 instead 
of 30. That just can’t be allowed!

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   399   400   401   402   403   404   405   406   ...   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