In order to complete the exercises and course project, you need any Java compiler and VM that supports JDK 6 or later.
You can download the latest version of the JDK by going to
JDK Download
The source code for the exercise and course project answers is included in a compressed download file for your convenience.
Also in this compressed file is the FileDumper program that you will use as the starting point for the course project. The download file is available in Windows, Macintosh, and Unix compressed formats. You can download the appropriate file by clicking the Resources link above.
Given the following code:
RandomAccessFile raf = new RandomAccessFile("c:\\temp\\test.txt", "rwd");
raf.writeChars("hello");
raf.close();
Which of the following statements are correct? (Assume that the code has appropriate security permissions.)
- If the file test.txt does not exist, an attempt will be made to create it.
- If the file test.txt does not exist, an exception will be thrown.
- If the file test.txt exists, an exception will be thrown.
- If the file test.txt exists, it will be overwritten and all the existing data will be lost.
- If the file test.txt exists, the given characters will be appended to the end of the existing data
Answer: a
Explanation:
d. Only the initial 5 characters (i.e. 10 bytes) of the file will be overwritten. Any existing data beyond the first 10 bytes will be left untouched.
e. When you open the file, the file pointer is at the first position.
So the given characters will be written at the beginning of the file.
The permitted values for the access mode and their meanings are:
- "r": Open for reading only. Invoking any of the write methods of the resulting object will cause an IOException to be thrown.
- "rw": Open for reading and writing. If the file does not already exist then an attempt will be made to create it.
- "rws": Open for reading and writing, as with "rw", and also require that every update to the file's content or metadata be written synchronously to the underlying storage device.
- "rwd": Open for reading and writing, as with "rw", and also require that every update to the file's content be written synchronously to the underlying storage device.
Question: What is the difference between rws and rwd?
- rws flushes the contents of the file and the modification date of the file.
- rwd flushes the contents of the file, but the modification date might not change until the file is closed.
- rw only flushes when you tell it to and doesn't change the modification date until you close the file. In addition, rwd is much slower for writes than rw, and rws is slower again.
Helpful books
While no book is required for this course, there are several that can offer additional information and serve as helpful resources.
An understanding of File I/O Fundamentals (Readers and Writers) is necessary to pass the 1Z0-804 Java Programmer 2 Examination.
The following book is recommended for this course.
Java Lambdas and Parallel Streams