Java File Class

Last Updated : 12 Feb 2026

File class in Java is used to work with files and directories. In this chapter, we will learn what the File class is, why it is used, its declaration, fields, constructors, methods, and multiple examples to clearly understand how file and directory operations work.

What is File Class in Java?

The File class is an abstract representation of file and directory pathnames. A pathname can be absolute (full path) or relative (path from the current directory).

The File class does not read or write file data, but it provides methods to:

  • Create files and directories
  • Delete or rename files and directories
  • Check file properties
  • List directory contents

Why is File Class Used?

The File class is used to:

  • Manage files and directories programmatically
  • Check file existence and permissions
  • Get file information like name, size, and path
  • Perform directory listing operations

It acts as a bridge between Java programs and the file system.

File Class Declaration

The File class belongs to the java.io package.

Fields of File Class

The following are commonly used static fields of the File class:

Field

Description

pathSeparator

System-dependent path separator as a string

pathSeparatorChar

System-dependent path separator character

separator

System-dependent name separator as a string

separatorChar

System-dependent name separator character

Constructors of File Class

The File class provides several constructors to create file or directory path objects.

1. File(String pathname)

This constructor creates a new File instance using the given pathname.

Here is the syntax:

2. File(String parent, String child)

This constructor creates a new File instance using a parent path and child path.

Here is the syntax:

3. File(File parent, String child)

This constructor creates a new File instance from a parent File object and child pathname.

Here is the syntax:

4. File(URI uri)

This constructor creates a new File instance by converting the given file: URI.

Here is the syntax:

Example of File Class Constructors

The following example demonstrates different ways to create File objects.

Methods of File Class

The File class provides the following methods to work with files and directories:

Modifier and TypeMethodDescription
static FilecreateTempFile(String prefix, String suffix)It creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
booleancreateNewFile()It atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
booleancanWrite()It tests whether the application can modify the file denoted by this abstract pathname.String[]
booleancanExecute()It tests whether the application can execute the file denoted by this abstract pathname.
booleancanRead()It tests whether the application can read the file denoted by this abstract pathname.
booleanisAbsolute()It tests whether this abstract pathname is absolute.
booleanisDirectory()It tests whether the file denoted by this abstract pathname is a directory.
booleanisFile()It tests whether the file denoted by this abstract pathname is a normal file.
StringgetName()It returns the name of the file or directory denoted by this abstract pathname.
StringgetParent()It returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
PathtoPath()It returns a java.nio.file.Path object constructed from the this abstract path.
URItoURI()It constructs a file: URI that represents this abstract pathname.
File[]listFiles()It returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname
longgetFreeSpace()It returns the number of unallocated bytes in the partition named by this abstract path name.
String[]list(FilenameFilter filter)It returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
booleanmkdir()It creates the directory named by this abstract pathname.

Examples of Java File Class

Practice the following examples to understand different operations of the File class.

Example 1: Creating a File

The following example demonstrates how to create a new file.

Output:

New file is created!

Example 2: Getting File Path Information

The following example demonstrates how to get absolute and canonical paths.

Output:

testFile1.txt
true
/home/Work/Project/File/testFile1.txt

Example 3: Listing Files in a Directory

The following example demonstrates how to list all file names in a directory.

Output:

info.properties
info.properties.rtf
.DS_Store
.localized
Alok news
apache-tomcat-9.0.0.M19
apache-tomcat-9.0.0.M19.tar
bestreturn_org.rtf
BIODATA.pages
BIODATA.pdf
BIODATA.png
struts2jars.zip
workspace

Example 4: Getting File Properties

The following example demonstrates how to read file properties such as size, hidden status, and write permission.

Output:

info.properties Can Write: true Is Hidden: false Length: 15 bytes
info.properties.rtf Can Write: true Is Hidden: false Length: 385 bytes
.DS_Store Can Write: true Is Hidden: true Length: 36868 bytes
.localized Can Write: true Is Hidden: true Length: 0 bytes
Alok news Can Write: true Is Hidden: false Length: 850 bytes
apache-tomcat-9.0.0.M19 Can Write: true Is Hidden: false Length: 476 bytes
apache-tomcat-9.0.0.M19.tar Can Write: true Is Hidden: false Length: 13711360 bytes
bestreturn_org.rtf Can Write: true Is Hidden: false Length: 389 bytes
BIODATA.pages Can Write: true Is Hidden: false Length: 707985 bytes
BIODATA.pdf Can Write: true Is Hidden: false Length: 69681 bytes
BIODATA.png Can Write: true Is Hidden: false Length: 282125 bytes
workspace Can Write: true Is Hidden: false Length: 1972 bytes

Note: File size, hidden status, and permissions may vary depending on the operating system and directory contents.