Creating File and Directory

Whenever the data is needed to be stored, a file is used to store the data. File is a collection of stored information that is arranged in string, rows, columns and lines etc. In this section, we will see how to create a file. This example takes the file name and text data for storing to the file.

For creating a new file createNewFile() method is used. This method returns a boolean value true if the file is created otherwise returns false. If the mentioned file for the specified directory already exists then the createNewFile() method returns the false otherwise the method creates the mentioned file and returns true.

Let’s see an example that checks the existence of a specified file.

File01.java

// Creating a new file
import java.io.*;

public class FileTest01 {

    public static void main(String[] args) {

        File f = null;

        try {
            f = new File("myfile.txt");

            if (!f.exists()) {
                f.createNewFile();
                System.out.println("New file \"myfile.txt\" has been created in the current directory");
                System.out.println("The absolute path of the file is: " + f.getAbsolutePath());
            } 
            else {
                System.out.println("File \"myfile.txt\" already exists");
            }
        } 
        catch (Exception e) {
            System.out.println(e);
        }

    }

}

This program checks whether the specified file “myfile.txt” exists or not using the exists() method. If it does not exist then a new file is created with same name to the current location. If we try to run this program again then after checking the existence of the file, it will not be created and we will see a message as shown in the output. The method getAbsolutePath() here returns the absolute path name in  the form of string.

 

Output:

C:\file>java File01
New file "myfile.txt" has been created in the current directory
The absolute path of the file is: C:\file\myfile.txt

C:\file>java File01
File "myfile.txt" already exists

 

 

Constructing a file name path during file creation

In Java, it is possible to set dynamic path, which is helpful for mapping local file name with the actual path of the file using the constructing filename path technique. As we have seen, how a file is created to the current directory where the program is run. Now we will see how the same program constructs a File object from a more complicated file name, using the static constant File.separator or File.separatorChar to specify the file name in a platform-independent way. If we are using MS Windows platform then the value of this separator is  “\“. Let’s see an example to create a file to the specified location.

File02.java

// Creating a new file after creating a new directory
import java.io.*;

public class FileTest02 {
    
    public static void main(String[] args) {
        
        String path = "C:\\file" + File.separator + "example";
        String fname = path + File.separator + "abc.txt";
        File f = new File(path);
        File f1 = new File(fname);
                
        try {
            f.mkdirs(); // creates a new directory
            f1.createNewFile(); // creates a new file
            System.out.println("New file \"abc.txt\" created to the specified location");
            System.out.println("The absolute path of the file is: " + f.getAbsolutePath());
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
       
    }
}

 

Output:

C:\file>java FileTest02
New file "abc.txt" created to the specified location
The absolute path of the file is: C:\file\example

 

Another program sets the dynamic path using File.separatorChar given below:

import java.io.*;

public class FileTest02a {

public static void main(String[] args) throws IOException {
String filepath = File.separatorChar + "techguru" + File.separatorChar + "speaks";
System.out.println("The path of the file is : " + filepath);
}

}

 

Output:

C:\file>java FileTest02a
The path of the file is : \techguru\speaks