Java Reference
In-Depth Information
1-9009-3323-4x
|
Wiper
Blade
Micro
Edge
|
30 |
24.68
1-3233-44923-7j
|
Parking
Brake
Cable
|
5 |
19.95
2-3399-6693-2m
|
Halogen
Bulb
H4
55/
60W
|
22 |
8.13
2-599-2029-6k
|
Turbo
Oil
Line
O-
Ring
|
26 |
1.55
3-1299-3299-9u
|
Air
Pump
Elec-
tric
|
9 |
202.00
Number of records = 5
Listing 8-7
relies on the try-with-resources statement to simplify the code—notice
try (PartsDB pdb = new PartsDB("parts.db"))
. To observe a sup-
(makesuretocommentout
raf.close();
inthatmethodorthecompilerwillcom-
plainaboutunreachablecode),anduncommentthethrowstatementin
Listing8-7
'stry
block.Thistime,whenyouruntheapplication,you'llnoticethefollowingtwolinesat
the end of the output:
java.io.IOException: I/O error
Suppressed = java.io.IOException: cannot close raf
Andthereyouhaveit:asimpleflatfiledatabase.Despiteitslackofsupportforad-
vanceddatabasefeaturessuchastransactionmanagement,aflatfiledatabasemightbe
all that your application requires.
Note
Tolearnmoreaboutflatfiledatabases,checkoutWikipedia's“Flatfiledata-
Streams
Alongwith
File
and
RandomAccessFile
,JavausesstreamstoperformI/Ooper-
ations.A
stream
isanorderedsequenceofbytesofarbitrarylength.Bytesflowoveran
output stream
fromanapplicationtoadestination,andflowoveran
input stream
from
a source to an application.
Figure 8-2
illustrates these flows.