Java IO File and Path Operations

Introduction

A file or a directory in the file system is represented by two abstract concepts in java. These abstract concepts are java.io.File and java.nio.file.Path. The File class represents a file in the file system whereas the interface Path represents the path string of the file. In this tutorial we look at various operations on File or Path. We get a handle on the File using

This does not create a file, but only points to the actual file. The file may or may not exist at that location. We can check the existence of the file at that location and also print the absolute file name.

Another way to create a file name from the name elements is

Check permissions on the file. Check whether the file is executable, readable and writable

Understanding File Path

Relative path, absolute path, cannonical path… The examples below explain all of these. It is also possible to get the name of the file using the File object or the name of the parent directory.

Creating a new File

A new file can be created using the function create new file. In the example below we create an abstract file called file2 and then create it. The function returns true if the file was created successfully and false if the file is already present.

A file can also be created using the java.nio.file.Files class. Most of the operations on these class are performed on the Path object. The Path can be obtained by using the toPath() method of the file. A new file can then be created by passing the appropriate File Attributes. The File attributes could be posix file permissions.

Another way to create a file is using org.apache.commons.io.FileUtils. it has a unix like ‘touch’ command that can be used to create a file

Creating a Directory

Line 1 createa a directory named ‘a’ using java.io.File. Directory creation in line 2 fails since directory b is not present. line 3 creates the directory and all missing directories

The java.nio.file.Files class can be used to create directories as well. Line 1 creates directory c. Line 2 creates directory b and another directory a inside it. FileSystems.getDefault().getPath(“”) returns a java.nio.file.Path object.

Creating temporary Files

The java.nio.file.Files and org.apache.commons.io.FileUtils has methods to create temporary files. The temporary location can be found by :

The temporary file can then be created. Line 1 creates a temporary file with prefix temp and suffix a. An ouput stream can be obtained on a temporary file such that the file is deleted on close.

Getting File Attributes

Check whether the java.io.File object represents a file and not a directory. There is a similar isDirectory() method.

Get the last modified time of a file

Get the total space of the partition in which where this file belongs and the free space available in the partition

The java.nio.file.Files class has some useful file attribute methods How to check if a file or directory exists. A symbolic link is followed by default. The default behaviour may be changed by using the NOFOLLOW_LINKS option

Get the last access time for a file or a directory

Get the basic attributes of a file

Get the last modified time for a file or a directory Get the basic attributes of a file

Get the creation time of a file or a directory Get the basic attributes of a file

Get the size of a file Get the basic attributes of a file

Get the size of a file in user readable form using apache commons io. The example below get the size of the temp directory

Check whether the file represents a directory Get the basic attributes of a file

Check whether the file represents a regular file

find out the owner of a file

Find out the content type of a file

File Store

The file store gives the storage system for the file

What is the name of the storage system of the file?

get the total storage space and total usable space of the partition in which the file resides

Leave a Comment