Java supports a class called File in the java.io package. It can be used to represent a file or a directory on the file system and can be used to perform common file-related operations like creating a file, deleting a file, etc. In this article, we will be taking a look at some of the common operations that can be performed on the java.io.File class.

  1. Creating a File object
  2. Creating a File and a Directory
  3. Obtaining information about a File
  4. Deleting and Renaming a File
  5. Obtaining a List of files

Java File Operations

Creating a File object

Before we learn about the File operations, let us take a look at how the File object can be created. The File class has several constructors which can be used to create a File object.

Sample Code

File f1 = new File("C:/myfile.txt");
File f2 = new File("C:/temp","myfile.txt");
File f3 = new File("C:/temp");
File f4 = new File(f3,"myfile.txt");
  • Line 1 creates a File object f1. This File constructor accepts a String parameter corresponding to the path of the file that needs to be created. Here, the value C:/myfile.txt is specified.
  • Line 2 creates a File object f2. This File constructor accepts two String parameters, the name of the directory in which the file is to be created (C:/temp) and the name of the file itself (myfile.txt). Note that the directory specified must already exist on the file system, otherwise an exception occurs.
  • Line 3 creates a File object f3 corresponding to the C:/temp path.
  • Line 4 creates a File object f4. This File constructor accepts a File object corresponding to the parent of the file to be created (f3 in this case) and a String value corresponding to the name of the file to be created (myFile.txt)

Creating a File and a Directory

The File class has a method called createNewFile. It can be used to create a file on the file system. It also has a method called mkDir. It can be used to create a directory on the file system.

Sample Code

File f1 = new File("C:/temp/myfile.txt");
boolean createdFile = f1.createNewFile();
System.out.println("created file:"+createdFile);
File f2 = new File("C:/temp2");
boolean createdDir = f2.mkdir();
System.out.println("created directory:"+createdDir);
  • Line 1 creates a File object f1 corresponding to the C:/temp/myfile.txt path.
  • Line 2 invokes the createNewFile method on f1. This creates the file at the specified path if the file does not exist and returns a true. If the file exists or if it is unable to create the file for some reason, it returns a false. If an error occurs while creating the file, it throws an IOException
  • Line 4 creates a File object f2 corresponding to the C:/temp2 path.
  • Line 5 invokes the mkDir method on f2. This creates the directory corresponding to the specified path if the directory does not exist and returns a true. If the directory exists or if it is unable to create the directory for some reason, it returns a false.

Output

created file:true
created directory:true

Obtaining information about a File

The File class has several methods that can be used to obtain information about a file like the name of the file, the name of the parent file, etc.

Sample Code

File f1 = new File("C:/temp/myfile.txt");
f1.createNewFile();
String fileName = f1.getName();
System.out.println("fileName="+fileName);
String parent = f1.getParent();
System.out.println("parent="+parent);
boolean exists = f1.exists();
System.out.println("exists="+exists);
boolean isFile = f1.isFile();
System.out.println("isFile="+isFile);
boolean isDirectory = f1.isDirectory();
System.out.println("isDirectory="+isDirectory)
  • Line 1 creates a File object f1 corresponding to the C:/temp/myfile.txt path and Line 2 creates this file.
  • Line 3 invokes the getName method on f1. This returns a String value corresponding to the name of the file.
  • Line 5 invokes the getParent method on f1. This returns a String corresponding to the path of the parent directory of the file.
  • Line 7 invokes the exists method on f1. This returns a boolean value that indicates whether the file exists on the file system or not.
  • Line 7 invokes the isFile method on f1. This returns a boolean value that indicates whether the underlying file object is a file or not. In this case, f1 represents a file on the file system, so this method will return a true.
  • Line 9 invokes the isDirectory method on f1. This returns a boolean value that indicates whether the underlying file object is a directory or not. In this case, f1 represents a file and not a directory so this method will return a false.

Output

fileName=myfile.txt
parent=C:\temp
exists=true
isFile=true
isDirectory=false

Deleting and Renaming a File

The File class has a method called delete. It can be used to delete a file on the file system. It also has a method called renameTo which can be used to rename a file on the file system.

Sample Code

File f1 = new File("C:/temp/myfile.txt");
f1.createNewFile();
boolean deleted = f1.delete();
System.out.println("deleted="+deleted);
File f2 = new File("C:/temp/myfile.txt");
f2.createNewFile();
File f3 = new File("C:/temp/myfile2.txt");
boolean renamed = f2.renameTo(f3);
System.out.println("renamed="+renamed);
  • Line 1 creates a File object f1 corresponding to the C:/temp/myfile.txt path and Line 2 creates this file.
  • Line 3 invokes the delete method on f1. This deletes the file at the specified path. It returns a boolean value that indicates whether the file was deleted successfully or not. Note that if the file object on which the delete method is invoked is a directory, then the directory needs to be empty in order to be deleted.
  • Line 5 creates a File object f2 corresponding to the C:/temp/myfile.txt path and Line 2 creates it.
  • Line 7 creates another File object called f3 corresponding to the C:/temp/myfile2.txt
  • Line 8 invokes the renameTo method on f2 passing in f3 as a parameter. This causes the underlying file to be renamed to the new name. It returns a boolean value which indicates whether the file was successfully renamed or not.

Output

deleted=false
renamed=false

Obtaining a List of files

The File class has a method called list. This returns a String array consisting of the names of the files or directories in the directory represented by the File object. It also has a method called listFiles. It returns a File array consisting of the files or directories in the directory represented by the File object.

Sample Code

File f1 = new File("C:/mydir");
String[] filesNames = f1.list();
for(String name:filesNames) {
 System.out.println(name);
}
File[] files = f1.listFiles();
for(File file:files) {
 System.out.println(file.getName());
}
  • Line 1 creates a File object f1 corresponding to the C:/myDir path which is a directory
  • Line 2 invokes the list method on f1. This returns a String array consisting of the names of the files at the specified path. Note that if the file object on which it is invoked does not represent a directory, then it returns a null.
  • Line 6 invokes the listFiles method on f1. This returns a File array. Thus, each file at the specified path is returned as a File object. Like the list method, the listFiles method also returns a null if the file object on which it is invoked is not a directory.

Output

file1.txt
file2.txt
file1.txt
file2.txt

Conclusion

So, in this article, we learnt about some of the operations that can be performed using the java.io.File class. We first learnt how to create a File object. We then saw how to create a file/directory. Next, we saw how to retrieve information about the file like the file name, the parent path, etc. Next, we saw how to rename/delete a file. Finally, we learnt about how to obtain a list of files in a particular directory.