Java Reference
In-Depth Information
private transitent String secretCreditCardNumber;
The reason why you might want to make fields transient is because they are stored
in an unencrypted format when they're serialized.
Finally, note that you can override two hidden methods: private void readObject
(ObjectInputStream s) and private void writeObject(ObjectOutputStream
s) in order to define a custom (de)serialization scheme. In most cases, the default
serialization scheme works just fine, though.
other streams
Finally , a multitude of various other streams exist. Consider for instance AudioInputStream , which
reads in audio-based data (sample frames). Or ZipOutputStream , which implements an output
stream for writing ZIP (compressed) files. The latter is a subclass of the FilterOutputStream ,
which acts as a superclass of other transforming output stream classes as well.
Most of these other streams subclass InputStream and OutputStream , i.e. byte streams, which you
have seen before. Browse through the Java API docs in order to get an overview of all the stream
types included in the standard Java API.
scanners
Earlier, you saw how you can perform advanced output formatting using the format method of the
PrintWriter and PrintStream stream classes. This might have left you wondering: what about
input? If a text file is formatted in a known manner, how can you easily read out all the data?
In fact, you've already seen a few ways to tackle this problem. If you don't mind your input data
being binary, you can use data or object streams to read in the various data types correctly. If your
data is given as text, you have seen how the BufferedReader class can help read the data line by
line. But what if a line of text contains different fragments you want to parse out?
Luckily, Java provides a simple means to break down input into various fragments—or tokens , as
they are called—and translate them according to their data type. The class that helps you do this is
the Scanner , found under java.util.Scanner . The following Try It Out shows how it works.
Scanning a Grocery List with prices
try it out
In this Try It Out, you use the Scanner class to read in text fragments and translate them according to
their data type.
1.
You will continue working in the same project as before. Create a new text file next to groceries.
txt called grocerieswithprices.txt with the following content:
apples, 5.33
bananas, 4.61
 
Search WWH ::




Custom Search