Tech and Media Labs
This site uses cookies to improve the user experience.




Java IO: File

Jakob Jenkov
Last update: 2014-11-15

The File class in the Java IO API gives you access to the underlying file system. Using the File class you can:

This text will tell you more about how.

Note: The File only gives you access to the file and file system meta data. If you need to read or write the content of files, you should do so using either FileInputStream, FileOutputStream or RandomAccessFile.

Note: If you are using Java NIO you will have to use the java.nio.FileChannel class instead (you can use both, but in case you want a pure Java NIO solution).

Instantiating a java.io.File

Before you can do anything with the file system or File class, you must obtain a File instance. Here is how that is done:

File file = new File("c:\\data\\input-file.txt");

Simple, right? The File class also has a few other constructors you can use to instantiate File instances in different ways.

Check if File Exists

Once you have instantiated a File object you can check if the corresponding file actually exists already. The File class constructor will not fail if the file does not already exists. You might want to create it now, right?

To check if the file exists, call the exists() method. Here is a simple example:

File file = new File("c:\\data\\input-file.txt");

boolean fileExists = file.exists();

Create a Directory if it Does Not Exist

You can use the File class to create directories if they don't already exists. The File class contains the method mkdir() and mkdirs() for that purpose.

The mkdir() method creates a single directory if it does not already exist. Here is an example:

File file = new File("c:\\users\\jakobjenkov\\newdir");

boolean dirCreated = file.mkdir();

Provided that the directory c:\users\jakobjenkov already exists, the above code will create a subdirectory of jakobjenkov named newdir. The mkdir() returns true if the directory was created, and false if not.

The mkdirs() will create all directories that are missing in the path the File object represents. Here is an example:

File file = new File("c:\\users\\jakobjenkov\\newdir");

boolean dirCreated = file.mkdirs();

Provided that the C drive exists, this example will create all the directories in the path c:\users\jakobjenkov\newdir. The mkdirs() method will return true if all the directories were created, and false if not.

File Length

To read the length of a file in bytes, call the length() method. Here is a simple example:

File file = new File("c:\\data\\input-file.txt");

long length = file.length();

Rename or Move File

To rename (or move) a file, call the method renameTo() on the File class. Here is a simple example:

File file = new File("c:\\data\\input-file.txt");

boolean success = file.renameTo(new File("c:\\data\\new-file.txt"));

As briefly mentioned earlier, the renameTo() method can also be used to move a file to a different directory. The new file name passed to the renameTo() method does not have to be in the same directory as the file was already residing in.

The renameTo() method returns boolean (true or false), indicating whether the renaming was successful. Renaming of moving a file may fail for various reasons, like the file being open, wrong file permissions etc.

Delete File

To delete a file call the delete() method. Here is a simple example:

File file = new File("c:\\data\\input-file.txt");

boolean success = file.delete();

The delete() method returns boolean (true or false), indicating whether the deletion was successful. Deleting a file may fail for various reasons, like the file being open, wrong file permissions etc.

Check if Path is File or Directory

A File object can point to both a file or a directory.

You can check if a File object points to a file or directory, by calling its isDirectory() method. This method returns true if the File points to a directory, and false if the File points to a file. Here is a simple example:

File file = new File("c:\\data");

boolean isDirectory = file.isDirectory();

Read List of Files in Directory

You can obtain a list of all the files in a directory by calling either the list() method or the listFiles() method. The list() method returns an array of String's with the file and / or directory names of directory the File object points to. The listFiles() returns an array of File objects representing the files and / or directories in the directory the File points to.

Here is a simple example:

File file = new File("c:\\data");

String[] fileNames = file.list();

File[]   files = file.listFiles();

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC