Determining the File Size – Java I/O: Part II

Determining the File Size

The method size() in the Files class is called at (1) in Example 21.4 to determine the size of the file denoted by the Path object. If the Path object denotes a directory, the method returns the size of the directory file and not the size of entries in the directory.

Click here to view code image

static long size(Path path) throws IOException

Returns the size of a file (in bytes). The size of files that are not regular files is unspecified, as it is implementation specific.

Determining the Kind of Directory Entry

The following methods in the Files class are called at (2), (3), and (4) in Example 21.4 to determine what kind of directory entry is denoted by the Path object.

Click here to view code image

static boolean isDirectory(Path path, LinkOption… options)
static boolean isRegularFile(Path path, LinkOption… options)
static boolean isSymbolicLink(Path path)

Return true if the directory entry is a directory, a regular file, or a symbolic link, respectively. They return false if the directory entry does not exist, or is not of the expected kind, or it is not possible to determine what kind of directory entry it is. In the first two methods, symbolic links are followed by default, unless the constant LinkOption.NOFOLLOW_LINKS is specified.

Determining File Accessibility

The methods in the Files class shown below are called at (5), (6), (7), and (8) in Example 21.4, respectively, to determine accessibility of the directory entry denoted by the Path object.

Click here to view code image

static boolean isReadable(Path path)
static boolean isWritable(Path path)
static boolean isExecutable(Path path)

Test whether a file is readable, writable, or executable, respectively. The file must exist and the JVM must have the appropriate privileges to access the file.

Note that the result returned by these method is immediately outdated. The outcome of subsequent attempts to access the file is not guaranteed, as concurrently running threads might change the conditions after the method returns.

Click here to view code image

static boolean isHidden(Path path) throws IOException

Determines whether or not a file is considered hidden. The exact definition of a hidden file is platform specific. On Unix platforms, files whose name begins with a period character (.) are considered to be hidden.

Categories: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *