— Scott McNealy, Sun Microsystems Chairman, President and ceo


Make an ObjectOutputStream



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

2
Make an ObjectOutputStream
ObjectOutputStream lets you write objects, 
but it can’t directly connect to a file. It 
needs to be fed a ‘helper’. This is actually 
called ‘chaining’ one stream to another. 


serialization 
and
file I/O
you are here
4
433
The Java I/O API has connection streams, that represent connections to destinations and 
sources such as fi les or network sockets, and chain streams that work only if chained to 
other streams. 
Often, it takes at least two streams hooked together to do something useful—one to 
represent the connection and another to call methods on. Why two? Because connection 
streams are usually too low-level. FileOutputStream (a connection stream), for example, 
has methods for writing bytes. But we don’t want to write bytes! We want to write objects, so 
we need a higher-level chain stream.
OK, then why not have just a single stream that does 
exactly what you want? One that lets 
you write objects but underneath converts them to bytes? Think good OO. Each class 
does one thing well. FileOutputStreams write bytes to a fi le. ObjectOutputStreams turn 
objects into data that can be written to a stream. So we make a FileOutputStream that lets 
us write to a fi le, and we hook an ObjectOutputStream (a chain stream) on the end of it. 
When we call writeObject() on the ObjectOutputStream, the object gets pumped into the 
stream and then moves to the FileOutputStream where it ultimately gets written as bytes 
to a fi le.
The ability to mix and match different combinations of connection and chain streams 
gives you tremendous fl exibility! If you were forced to use only a single stream class, you’d 
be at the mercy of the API designers, hoping they’d thought of everything you might ever 
want to do. But with chaining, you can patch together your own custom chains.
Data moves in streams from one place to another.
Source
Destination
Object
ObjectOutputStream 
(a chain stream)
is written to
object is fl attened (serialized)
FileOutputStream
(a connection stream)
011010010110111001
object is written as bytes to
01101001
01101110
01
File
destination
Connection 
streams represent 
a connection 
to a source or 
destination (file, 
socket, etc.) while 
chain streams 
can’t connect on 
their own and must 
be chained to a 
connection stream.
is chained to



Download 40,57 Mb.

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