Example: Getting File and Directory Information

Figure 15.2 prompts the user to enter a file or directory name, then uses classes Paths, Path, Files and DirectoryStream to output information about that file or directory. The program begins by prompting the user for a file or directory (line 16). Line 19 inputs the filename or directory name and passes it to Paths static method get, which converts the String to a Path. Line 21 invokes Files static method exists, which receives a Path and determines whether it exists (either as a file or as a directory) on disk. If the name does not exist, control proceeds to line 49, which displays a message containing the Path’s String representation followed by “does not exist.” Otherwise, lines 24–45 execute: ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.