OCP Flashcards – 9. Java File I/O (NIO.2)

OCP Exam Objective 9 – Java File I/O (NIO.2)

Flashcard OCP.9.Java.File.IO.(NIO2).001

What are differences between Java IO, NIO and NIO.2?

Click here to view answer
[Based on Oracle Certified Professional Java SE7 Programmer Exams 1Z0-804 & 1Z0-805 – A Comprehensive OCJP7 Certification Guide by S.G.Ganesh and Tushar Sharma, Chapter 9, A Quick History of I/O APIs, p.251]
Java started initially (i.e., from 1.0 onwards) by offering the File class (defined in the java.io package) to access file systems. This object represents a file/directory and did allow you to perform some operations such as checking if a file/directory exists, get its properties and delete it. It had, though, some shortcomings. To name a few:

  • The File class lacked some important functionality, such as a copy method.
  • It also defined many methods that returned boolean. As one can imagine, in case of an error, false was returned rather than throwing an exception. The developer had, indeed, no way of knowing why it failed.
  • Did not provide good handling on support of symbolic links.
  • A limited set of file attributes was provided.

To overcome these problems, java.nio package was introduced in Java 1.4. The key features were:

  • Channels and Selectors: A channel is an abstraction on lower-level file system features, e.g. memory-mapped files.
  • Buffers: Buffering for all primitive classes (except for boolean).
  • Charset: Charset (java.nio.charset), encoders, and decoders to map bytes and Unicode symbols

Java 7 introduced a new set of I/O APIs called NIO.2 that offer convenient ways to perform operations related to the file system including a better support for handling symbolic links, file attributes access, etc. through the Path interface and the utility classes such as Paths and Files defined in the java.nio.file package.


IO lacked many features, NIO is a more abstract low-level data IO API, and NIO.2 focuses primarily on file management. Starting from 1.8, NIO.2 also supports Stream API operations.


Flashcard OCP.9.Java.File.IO.(NIO2).002

What is a Path object?

Click here to view answer
It’s a programming abstraction representing the path of a file/directory.


Flashcard OCP.9.Java.File.IO.(NIO2).003

How would you create an instance of Path?

Click here to view answer
By invoking the Paths#get() method.


Flashcard OCP.9.Java.File.IO.(NIO2).004

Which Files method traverses the file tree including sub-directories, starting from a given point?

Click here to view answer


Flashcard OCP.9.Java.File.IO.(NIO2).005

What is the difference between File#getName() and Path#getName(int) methods?

Click here to view answer
Since the File object is an abstract representation of file and directory pathnames, getName() returns a String containing the name of the file or directory denoted by this abstract pathname.

The Path object represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter. The getName(int index) returns a Path object containing the specified name element of this path. The element that is closest to the root in the directory hierarchy has index 0.


Flashcard OCP.9.Java.File.IO.(NIO2).006

Which element will be retrieved from the path C:\\Try_Java\\igor\\host\\Test.java with this invocation: getName(3)?

Click here to view answer


Flashcard OCP.9.Java.File.IO.(NIO2).007

What class would you use to invoke the copy(), move() and delete() methods in order to copy, move and delete files, respectively?

Click here to view answer
The Files class


Flashcard OCP.9.Java.File.IO.(NIO2).008

When and why would you want to use the StandardCopyOption.REPLACE_EXISTING?

Click here to view answer
When invoking the Files.copy(Path source, Path target, CopyOption… options) or Files.move(Path source, Path target, CopyOption… options) methods because, by default, these methods fail if the target already exists.


Flashcard OCP.9.Java.File.IO.(NIO2).009

Why would you want to use the StandardCopyOption.ATOMIC_MOVE?

Click here to view answer
When moving a file. If the move is performed as a non-atomic operation, and an IOException is thrown, then the state of the files is not defined. The original file and the target file may both exist, the target file may be incomplete or some of its file attributes may not been copied from the original file.


Flashcard OCP.9.Java.File.IO.(NIO2).010

Which Path method converts a relative Path object into an absolute Path object?

Click here to view answer


Flashcard OCP.9.Java.File.IO.(NIO2).011

Which Path method is used to determine the relative path between two Path objects?

Click here to view answer


Flashcard OCP.9.Java.File.IO.(NIO2).012

Which Path method removes redundant name elements such as . and .. from this Path object?

Click here to view answer


Flashcard OCP.9.Java.File.IO.(NIO2).013

Which Files method returns a Stream<Path> whose elements are the entries in the directory (that is, files & subfolders)?

Click here to view answer
list(Path dir). Note that this method does not recursively traverse the directories in the given Path.


Flashcard OCP.9.Java.File.IO.(NIO2).014

Which Path method is used to append a partial path to an existing Path object?

Click here to view answer


Flashcard OCP.9.Java.File.IO.(NIO2).015

What is the difference between Files.readAllLines() and Files.lines()?

Click here to view answer
Files.readAllLines(Path path) returns a List<String> and is intended for simple cases where it is convenient to read all lines in a single operation. It is not intended for reading in large files.

Files.lines(Path path) also reads the contents of a file but returns a Stream<String>. Unlike readAllLines(), this method does not read all lines into a List, but instead populates lazily as the stream is consumed.

Leave Comment

Your email address will not be published.