Readers Writers   «Prev  Next»


Lesson 4Java Readers Writers Course Requirements
ObjectiveUnderstand how to get the most from Readers and Writers

Java Readers Writers Course Requirements

Everything you need to complete this course is available online.

Programming Software

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

Course download Files

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.)
  1. If the file test.txt does not exist, an attempt will be made to create it.
  2. If the file test.txt does not exist, an exception will be thrown.
  3. If the file test.txt exists, an exception will be thrown.
  4. If the file test.txt exists, it will be overwritten and all the existing data will be lost.
  5. 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:
  1. "r": Open for reading only. Invoking any of the write methods of the resulting object will cause an IOException to be thrown.
  2. "rw": Open for reading and writing. If the file does not already exist then an attempt will be made to create it.
  3. "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.
  4. "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?
  1. rws flushes the contents of the file and the modification date of the file.
  2. rwd flushes the contents of the file, but the modification date might not change until the file is closed.
  3. 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