There is a
second parameter in the constructor for the FileOutputStream which would allow a program to append to
an existing file, but we have not used that here.
With the read() method of the BufferedInputStream, either a byte of data is returned, or, if the
program encounters the EOF, a binary -1 is returned. As long as the read() continues to return bytes, the
program calls write() to copy the data. Though you see individual bytes being read and written, the transfer
is much more efficient than you might think, because the InputStream and OutputStream are buffered.
SCANNER
Often we need to read information from the user of the program. We might prompt the user, for example,
to ask them what kind of car they drive. We can ask the question of the user by displaying the question using
System.out.println(). We have shown many examples of displaying information on the screen.
Java provides a class called Scanner that makes it easy to read information from the keyboard into the
program. To create a scanner object for reading from the keyboard, we simply pass the standard input ???stream???
to the constructor for a new Scanner. For example, here is a snippet of code to create a new Scanner for
reading from the keyboard, and then to use the Scanner to read a floating-point number:
.
Pages:
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245