Chapter 14 I/O Flashcards
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.
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.
Referencing Files and Directories
File class and Path interface
Conceptualizing the File System
- 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.
Operating System File Separators
- 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"));
absolute path
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.
relative path
the relative path of a file or directory is the path from the current working directory to the file or directory.
Determining whether a path is relative
or absolute
is file-system dependent.
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.
File-system symbols
- . A reference to the current directory
- .. A reference to the parent of the current directory
symbolic link
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.
Creating a File or Path
- 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
Creating a File
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.
Creating a Path
- 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 aget()
method to do the same thing. -
Files helper class
, which can check if the file exists on the file system.
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 uniform resource identifier (URI) is a string of characters that identifies a resource.
Switching between File and Path
File file = new File("rabbit"); Path nowPath = file.toPath(); File backToFile = nowPath.toFile();