Java File object represents file and directory pathnames. Different operating systems uses system dependent strings to name the file and directories. The File class represents these file and directory paths in an abstract manner.

The pathnames denoted by the File object may be relative or absolute. It may differ for different operating system, for example, a file name starting with ‘/’ in Unix environment is always an absolute path, while in Windows it starts with drive letter like C: or D:.

The Java File class provides various methods to create, read and delete files and directories. Here are some of the very useful methods provided by the File class.

1) Methods to modify file or directory attributes
The File class provides several methods to change the file or directory attributes like setReadOnly, setWritable, setHidden, setLastModified and setExecutable.

2) Methods to create files and directories
The File class provides methods to create a file like createNewFile and createTempFile. It also provides methods to create parent directories like mkdir and mkdirs.

3) The list and listFiles methods provide the functionality to list directory contents.

4) The getTotalSpace, getFreeSpace and getUsableSpace methods allows us to get file or directory size.

5) The canRead, canWrite, canExecute methods allows us to check whether the code has various file system permissions.

Below given are some of the Java File examples which shows how to use Java File object to access file system.

Java File Examples

Join 1000+ fellow learners! Enter your email address below: