Chapter 14 I/O Flashcards

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

NOTE

> [!NOTE]
NIO stands for non-blocking input/output API and is sometimes referred to as new I/O. The exam covers NIO version 2. There was a version 1 that covered channels, but it is not on the exam.

A

NIO stands for non-blocking input/output API and is sometimes referred to as new I/O

The exam covers NIO version 2.
There was a version 1 that covered channels, but it is not on the exam.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Referencing Files and Directories

A

File class and Path interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Conceptualizing the File System

A
  • A file within the storage device holds data. Files are organized into hierarchies using directories.
  • A directory is a location that can contain files as well as other directories.
  • The file system is in charge of reading and writing data within a computer.
  • For the exam, you just need to know how to issue commands using the Java APIs.
  • The JVM will automatically connect to the local file system, allowing you to perform the same operations across multiple platforms.
  • the root directory is the topmost directory in the file system, from which all files and directories inherit.
  • A path is a representation of a file or directory within a file system.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Operating System File Separators

A
  • Unix-based systems use the forward slash, /
  • Windows-based systems use the backslash, \, character.
  • Java offers a system property to retrieve the local separator character for the current environment:
    System.out.print(System.getProperty("file.separator"));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

absolute path

A

The absolute path of a file or directory is the full path from the root directory to the file or directory, including all subdirectories that contain the file or directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

relative path

A

the relative path of a file or directory is the path from the current working directory to the file or directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Determining whether a path is relative or absolute is file-system dependent.

A

To match the exam, we adopt the following conventions:
* If a path starts with a forward slash (/), it is absolute, with / as the root directory, such as /bird/parrot.png.
* If a path starts with a drive letter (c:), it is absolute, with the drive letter as the root directory, such as C:/bird/info.
* Otherwise, it is a relative path, such as bird/parrot.png.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

File-system symbols

A
  • . A reference to the current directory
  • .. A reference to the parent of the current directory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

symbolic link

A

A symbolic link is a special file within a file system that serves as a reference or pointer to another file or directory. Suppose we have a symbolic link from /zoo/user/favorite to /fish/shark. The shark folder and its elements can be accessed directly or via the symbolic link. For example, the following paths reference the same file:
/fish/shark/swim.txt
/zoo/user/favorite/swim.txt

While the I/O APIs do not support symbolic links, NIO.2 includes full support for creating, detecting, and navigating symbolic links within the file system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Creating a File or Path

A
  • legacy I/O, this is the java.io.File class
  • NIO.2, it is the java.nio.file.Path interface.
    The File class and Path interface cannot read or write data within a file, although they are passed as a reference to other classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Creating a File

A

The File class is created by calling its constructor.
This code shows three different constructors:
File zooFile1 = new File("/home/tiger/data/stripes.txt");
File zooFile2 = new File("/home/tiger", "data/stripes.txt");
File parent = new File("/home/tiger");
File zooFile3 = new File(parent, "data/stripes.txt");
System.out.println(zooFile1.exists()); //if file exists

If we passed null as the parent to the final constructor, it would be ignored, and the method would behave the same way as the single String constructor. For fun, we also show how to tell if the file exists on the file system.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Creating a Path

A
  • Path is an interface, we can’t create an instance directly. After all, interfaces don’t have constructors!
  • All four of these examples point to the same reference on disk:
Path zooPath1 = Path.of("/home/tiger/data/stripes.txt");
Path zooPath2 = Path.of("/home", "tiger", "data", "stripes.txt");
Path zooPath3 = Paths.get("/home/tiger/data/stripes.txt");
Path zooPath4 = Paths.get("/home", "tiger", "data", "stripes.txt");
System.out.println(Files.exists(zooPath1));
  • The Path.of() method was introduced in Java 11 as a static method on the interface.
  • The Paths factory class also provides a get() method to do the same thing.
  • Files helper class, which can check if the file exists on the file system.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

NOTE

> [!NOTE]
You might notice that both the I/O and NIO.2 classes can interact with a URI. A uniform resource identifier (URI) is a string of characters that identifies a resource. It begins with a schema that indicates the resource type, followed by a path value such as file:// for local file systems and http://, https://, and ftp:// for remote file systems.

A

A uniform resource identifier (URI) is a string of characters that identifies a resource.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Switching between File and Path

A
File file = new File("rabbit");
Path nowPath = file.toPath();
File backToFile = nowPath.toFile();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Obtaining a Path from the FileSystems Class

Path zooPath1 = FileSystems.getDefault().getPath("/home/tiger/data/stripes.txt");
Path zooPath2 = FileSystems.getDefault().getPath("/home", "tiger", "data", "stripes.txt");
A

The FileSystems class creates instances of the abstract FileSystem class. The latter includes methods for working with the file system directly. Both Paths.get() and Path.of() are shortcuts for this FileSystem method.

Path zooPath1 = FileSystems.getDefault().getPath("/home/tiger/data/stripes.txt");
Path zooPath2 = FileSystems.getDefault().getPath("/home", "tiger", "data", "stripes.txt");
17
Q

Reviewing I/O and NIO.2 Relationships

A
  • The model for I/O is smaller, and you only need to understand the File class.
  • NIO.2 has more features and makes extensive use of the factory pattern.
  • Many of your interactions with NIO.2 will require two types: an abstract class or interface and a factory or helper class.

FIGURE 14.3 I/O and NIO.2 class and interface relationships

  • In particular, keep an eye on whether the class name is singular or plural.
  • Classes with plural names include methods to create or operate on class/interface instances with singular names.
  • Remember, as a convenience (and source of confusion), a Path can also be created from the Path interface using the static factory of() method.
18
Q

NOTE

> [!NOTE]
The java.io.File is the I/O class, while Files is an NIO.2 helper class.
Files operates on Path instances, not java.io.File instances.
We know this is confusing, but they are from completely different APIs!

A
  • The java.io.File is the I/O class,
  • while Files is an NIO.2 helper class.
  • Files operates on Path instances,
19
Q

Options for creating File and Path

A

java.io.File and
java.nio.file.Path

20
Q

Operating on File and Path

A