ptg7068951
288 HOUR 20: Reading and Writing Files
Some MP3 files either don’t contain ID3 information at all or contain ID3
information in a different format than the application can read.
The file Come On and Gettit.mp3contains readable ID3 information if
you created it from a copy of the Eccentric SoulCD that you purchased
because programs that create MP3 files from audio CDs read song infor-
mation from a music industry database called CDDB.
After everything related to the ID3 information has been read from the
MP3’s file input stream, the stream is closed in Line 23. You should always
close streams when you are finished with them to conserve resources in
the Java interpreter.
Buffered Input Streams
One of the ways to improve the performance of a program that reads input
streams is to buffer the input. Buffering is the process of saving data in
memory for use later when a program needs it. When a Java program
needs data from a buffered input stream, it looks in the buffer first, which
is faster than reading from a source such as a file.
To use a buffered input stream, you create an input stream such as a
FileInputStreamobject, and then use that object to create a buffered
stream. Call the BufferedInputStream(InputStream)constructor with the
input stream as the only argument. Data is buffered as it is read from the
input stream.
To read from a buffered stream, call its read()method with no arguments.
An integer from 0 to 255 is returned and represents the next byte of data in
the stream. If no more bytes are available, –1 is returned instead.
As a demonstration of buffered streams, the next program you create adds
a feature to Java that many programmers miss from other languages they
have used: console input.
Console input is the ability to read characters from the console (also
known as the command-line) while running an application.
The Systemclass, which contains the outvariable used in the
System.out.print()and System.out.println()statements, has a class
variable called in that represents an InputStreamobject. This object
receives input from the keyboard and makes it available as a stream.
NOTE
Yo u m i g h t b e t e m p t e d t o f i n d a
copy of Come On and
Gettit.mp3on a service such
as BitTorrent,one of the most
popular file-sharing services. I
can understand this temptation
perfectly where “Come On and
Gettit” is concerned. However,
according to the Recording
Industry Association of
America,anyone who down-
loads MP3 files for music CDs
you do not own will immediately
burst into flame. Eccentric Soul
is available from Amazon.com,
eBay,Apple iTunes,and other
leading retailers.