The Path class creates objects that contain information about files and directories, such as sizes, locations, creation dates, and is used to check whether a file or directory exists.
The Files class performs operation on files and directories, such as determining their attributes, creating input and output streams, and deleting them.
To use both the Path and Files classes, add the following statement.
import java.nio.file.*;
To create and define a Path, usePaths class and its get() method.
A absolute path is a complete path; it does not require any other information to locate a file on a system.
Example of absolute file: C:\Java\Chapter8\sample.txt
A relativepathdependson other path information
Examples of relative path: sample.txt, Chapter8\sample.txt, Java\Chapter8.
To convert a relativepath to an absolutepath, the toAbsolutePath() method is used.
To verifyifafileexists and if a program canaccess it when needed, the checkAccess() method is used.
The delete() method of the Files class acceptsaPathparameter and removes the lastelement (file or directory) in a path or throws an exception if the deletion is unsuccessful.
file does not exists = NoSuchFileException
attempt to delete = DirectoryNotEmptyException
attempt to delete without permission = SecurityException
other input/output error cause IOException
deleteIfExists() - remove a file without encountering an exception if the file does not exists.
readAttributes() - To retrieve useful information.
readAttributes() returns an instance of the BasicFileAttributes.class.