Working with Files and Directories in Java

Files and directories are a fundamental part of any operating system, and Java provides several ways to interact with them. In this blog post, we’ll take a look at how to work with files and directories in Java, including how to read and write to files, how to create, delete, and list directories, and how to check for the existence of a file or directory.

Read contents from the file

To read from a file, we can use the BufferedReader class. Let’s see the code to read the contents of a file line by line below:

import java.io.*;

public class Main {
    public static void main(String[] args) {
        // Create a BufferedReader to read from a file
        try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
            // Read each line from the file
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Create file

To write a file, we can use the BufferedWriter class.

Let’s see an example of how to write a string to a file:

import java.io.*;

public class Main {
    public static void main(String[] args) {
        // Create a BufferedWriter to write to a file
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"))) {
            // Write a string to the file
            bw.write("Hello, world!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Create directory

To create a new directory, we can use the mkdir() method of the File class. This method creates a single directory and returns a boolean indicating whether the directory was successfully created.

Following is an example of how to use mkdir():

import java.io.File;

public class Main {
    public static void main(String[] args) {
        // Create a new directory
        File dir = new File("newdir");
        if (dir.mkdir()) {
            System.out.println("Directory created");
        } else {
            System.out.println("Directory not created");
        }
    }
}

Delete directory

To delete a directory, we can use the delete() method of the File class. This method deletes a single directory and returns a boolean indicating whether the directory was successfully deleted.

Following is an example of how to use delete():

import java.io.File;

public class Main {
    public static void main(String[] args) {
        // Delete a directory
        File dir = new File("newdir");
        if (dir.delete()) {
            System.out.println("Directory deleted");
        } else {
            System.out.println("Directory not deleted");
        }
    }
}

List the directory to see content

To list the contents of a directory, we can use the list() method of the File class. This method returns an array of strings containing the names of the files and directories in the given directory.

Following is an example of how to use list():

import java.io.File;

public class Main {
    public static void main(String[] args) {
        // List the contents of a directory
        File dir = new File(".");
        String[] contents = dir.list();
        for (String s : contents) {
            System.out.println(s);
        }
    }
}

Check the existence of a file

We can use the exists() method of the File class to check for the existence of a file or directory. This method returns a boolean indicating whether the file or directory exists.

Example of how to use exists():

import java.io.File;

public class Main {
    public static void main(String[] args) {
        // Check for the existence of a file
        File file = new File("file.txt");
        if (file.exists()) {
            System.out.println("File exists");
        } else {
            System.out.println("File does not exist");
        }
    }
}

In this blog post, we looked at how to work with files and directories in Java. We saw how to read and write to files, how to create, delete, and list directories, and how to check for the existence of a file or directory. With these techniques, you should be able to easily work with files and directories in your Java programs.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments