Introduction To Java File Class

A class object is used to represent directory or file in java, You can use it to do any operation to the file or directory in the system. The action you can take include create, move, copy, delete etc. Below i will show you how to do such actions in java.

How to create file or directory in java?

There are 4 constructor methods for class. You can use any of them to build a new File object.

//pathname: Absolute or relative path name.
File(String pathname)
//parent: Parent directory Object. child: String name which exist under parent directory.
File(File parent, String child)
//parent: String value of parent directory. child: String name which exist under parent directory. 
File(String parent, String child)
//uri: uniform resource identifier of the networkfile. 
File(URI uri)

Input parameter pathname is the absolute path of the file such as C:\Workspace\test.txt.  It is also can be a path name relative to current working directory, for example “test.txt”.

Input parameter uri is the uniform resource identifier, it is used to locate a file on the network. For example:

Suppose we want to create 3 file in windows. The path is listed below.


Following is the code example to implement above tasks.

 //Initiate java FileObject.
 File testFile = new File("C:\\Workspace\\test0.txt");
 //Create the new one.
 //Initiate parent directory.
 File parentDir = new File("C:\\Workspace\\JavaFileExample");
 //Create parent directory first.
 //Initiate child
 File testFile1 = new File(parentDir, "test1.txt");
 //Create child.
 //Initiate java FileObject
 File testFile2 = new File("C:\\Workspace", "test2.txt");
 //Create the new one.
 testFile2.createNewFile(); class provides a number of methods that you can use to perform different operations. You can use those methods to create new, copy exist and delete files, you can also use it to create a directory, check whether it is a directory or not, get attributes etc. You can click here to get full description about File class’s method description.

You can check whether it is exist or not use following code.

 File testFile = new File("C:\\Workspace\\test0.txt");
 File testFile1 = new File("C:\\Workspace\\test1.txt");
 //Exists() method is used to check whether it is exist or not.
 System.out.println("test0.txt exist.");
 System.out.println("test0.txt dose not exist.");
 System.out.println("test1.txt exist.");
 System.out.println("test1.txt dose not exist.");

Get or set the current java process working directory

Current working directory is the directory that java command has been invoked inside file system.

You can read system property “user.dir”‘s value to get current java process working directory as following.

 //Get current java process working directory.
 String javaCurrentWorkingDir = System.getProperty("user.dir");
 System.out.println("Curent java working directory is " + javaCurrentWorkingDir);

You can also change “user.dir”‘s value use System.setProperty() method.

//Set current java process working directory.
 System.setProperty("user.dir", "C:\\Workspace");

Difference between absolute and canonical file path?

Absolute path is used to identify a file inside the system uniquely. Following is some examples.


Relative path is used to identify a file relative to it’s parent directory. Following is examples


Canonical path is sure an absolute path. But it cleans the file path. It resolving stuff like ..\ or .\ and symlinks (on unixes). Following is canonical path examples.

 //This is canonical path
 //This is not a canonical path because of .\, but it is a absolute path
 //This is not a canonical path because of ..\, but it is a absolute path

Following is example code in java to get absolute and canonical file path.

 //Initiate object
 File testFile = new File("C:\\Workspace\\.\\test0.txt");
 //Get absolute path 
 String absolutePath = testFile.getAbsolutePath();
 //Get canonical path
 String canonicalPath = testFile.getCanonicalPath();
 //Get name.
 System.out.println("Name is " + testFile.getName());
 //Check existence.
 System.out.println("If exist ? " + testFile.exists());
 //Check whether is directory or not.
 System.out.println("Is a directory ? " + testFile.isDirectory());
 System.out.println("Absolute path is " + absolutePath);
 System.out.println("Canonical path is " + canonicalPath);

Difference between File.separator and File.pathSeparator

Can be used to split individual paths in file paths list. For example the PATH environment variable on windows. A semicolon(;)  is used to separate file paths in PATH environment variable, then File.pathSeparator is semicolon(;) in Windows.

There has different separator in different operating system to split two parts in file path name value.
UNIX use slash(/) while windows use backslash(\).
File.seperator is a constant variable which is used as separator string for file path system dependently.
You can make your java program run on different platforms easily if you use File.seperator.

[download id=”371″]

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.