Some Operations on Files
In this section, we will learn about some important operations on files such as creating directories or sub-directories, deleting a file or directory, getting the parent and current directory, identifying a file or directory and so on.
Deleting a File or Directory
Here, we will learn how a specified file or directory is deleted after checking the existence of the file. This topic is related to the I/O operation of java.io package. In this example we are using File class of java.io package. The File class is an abstract representation of file and directory path names.
Explanation
This program takes an input for the file name to be deleted and deletes the specified file if that exists. We will use the delete() method to delete the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. This method returns true if and only if the file or directory is successfully deleted; false otherwise. Code of the program is given below:
FileTest11.java
// Delete an existing file or directory
import java.io.*;
public class FileTest11 {
public static void main(String[] args) throws IOException {
System.out.print("Enter file name : ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
File f = new File(in.readLine());
if (f.exists()) {
boolean success = f.delete();
if (!success) {
System.out.println("Deletion failed.");
System.exit(0);
}
else {
System.out.println("File deleted.");
}
}
else {
System.out.println("File does not exist.");
System.exit(0);
}
}
}
Output:
C:\file>java File11
Enter file name : myps.txt
File deleted.
C:\file>java File11
Enter file name : C:\file\example
File deleted.
Getting the Parent & Current Directory and Creating Directories
In this topic, we will learn how to get the current working directory as well as the parent directory name. Java uses the getProperty() method of the System class of the java.lang package to return the system properties according to the indicated argument. Here, the mentioned indicated argument is “user.dir” which indicates about the getting the current directory name. The getParent( ) method of the File class object is used to find the parent directory name of the current directory.
This program also explains how a directory or sub-directories are created. We will use the File class to crate the directory. Here is the code of the program:
FileTest12.java
// Get the Parent and Current Working Directory
import java.io.*;
public class FileTest12 {
private static void dirlist(String fname) {
File dir = new File(fname);
String parentpath = dir.getParent();
System.out.println("Current Directory : " + dir);
System.out.println("Parent Directory : " + parentpath);
}
public static void main(String[] args) {
String currentdir = System.getProperty("user.dir");
dirlist(currentdir);
//creating a directory and sub-directories
try {
String strDirectoy = "sample";
String strManyDirectories = "dir1/dir2/dir3";
// Create one directory
boolean success = (new File(strDirectoy)).mkdir();
if (success) {
System.out.println("Directory: " + strDirectoy + " created");
}
// Create multiple directories
success = (new File(strManyDirectories)).mkdirs();
if (success) {
System.out.println("Directories: " + strManyDirectories + " created");
}
}
catch (Exception e) { //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Output:
C:\file>java File12
Current Directory : C:\file
Parent Directory : C:\
Directory: sample created
Directories: dir1/dir2/dir3 created
Checking a File or Directory
In this topic, we will check whether the user input denotes a file name or directory name. For this, we will use isFile() and isDirectory() methods of the File class respectively. Code of the program is given below:
FileTest13.java
// Checking whether a file or directory
import java.io.*;
public class FileTest13 {
public static void main(String[] args) throws IOException {
System.out.print("Enter file or directory name : ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
File name = new File(in.readLine());
if(name.isFile()) {
System.out.println("It is a file");
}
else if(name.isDirectory()) {
System.out.println("It is a directory");
}
else {
System.out.println("Neither file nor directory!");
}
}
}
Output:
C:\file>java File13
Enter file or directory name : myfile.txt
It is a file
C:\file>java File13
Enter file or directory name : C:\file
It is a directory
C:\file>java File13
Enter file or directory name : not-a-file
Neither file nor directory!