Java Reference
In-Depth Information
Summary
Applicationsofteninteractwiththefilesystemtooutputdatatoand/orinputdatafrom
files.Java'sstandardclasslibrarysupportsfilesystemaccessviaitsclassic
File
,
Ran-
domAccessFile
, stream, and writer/reader APIs.
Javaoffersaccesstotheunderlyingplatform'savailablefilesystem(s)viaitsconcrete
File
class.
File
instancescontaintheabstractpathnamesoffilesanddirectoriesthat
may or may not exist in their filesystems.
Filescanbeopenedforrandomaccessinwhichamixtureofwriteandreadoperations
canoccuruntilthefileisclosed.Javasupportsthisrandomaccessbyprovidingthecon-
crete
RandomAccessFile
class.
JavausesstreamstoperformI/Ooperations.Astreamisanorderedsequenceofbytes
ofarbitrary length. Bytes flow over an output stream from an application to a destina-
tion, and flow over an input stream from a source to an application.
The
java.io
package provides several output stream and input stream classes
that are descendents of the abstract
OutputStream
and
InputStream
classes.
Examples of subclasses include
FileOutputStream
and
BufferedIn-
putStream
.
Java'sstreamclassesaregoodforstreamingsequencesofbytes,butarenotgoodfor
streamingsequencesofcharactersbecausebytesandcharactersaretwodifferentthings,
and because byte streams have no knowledge of character sets and encodings.
Ifyouneedtostreamcharacters,youshouldtakeadvantageofJava'swriterandread-
erclasses,whichweredesignedtosupportcharacterI/O(theyworkwith
char
instead
of
byte
).Furthermore,thewriterandreaderclassestakecharacterencodingsintoac-
count.
The
java.io
package provides several writer and reader classes that are descend-
ents of the abstract
Writer
and
Reader
classes. Examples of subclasses include
OutputStreamWriter
,
FileWriter
,
InputStreamReader
,
FileReader
,
and
BufferedReader
.
Aswellasfilesystems,applicationsoftenmustinteractwithnetworksanddatabases.
Chapter9
providesanintroductiontothestandardclasslibrary'snetwork-orientedand
database-oriented APIs.