Skip to main content

I/O in Java - Directory operations (Part 4)

In the previous parts of this series I/O in Java, we discussed various aspects of file handling in Java and the classes that are normally used.

Today we will focus on two directory operations - Copy contents of one directory to another directory and recursively delete the contents of a given directory.

We will now see the sample code to perform these two operations. The code is fairly simple and self-explanatory.

Copy directories and their contents

Below code snippet can be used to perform this operation - 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
private static void copyDirectory(String sourceFolderPath, String destinationFolderPath) {

        System.out.println("Copying process starts...");

        try {
            // Getting file reference for source and destination folders
            File sourceFolder = new File(sourceFolderPath);
            File destinationFolder = new File(destinationFolderPath);

            // Check if the sourceFolder is a directory or file
            if (sourceFolder.isDirectory()) {

                // Check if destinationFolder is present. If not, then create it
                if (!destinationFolder.exists()) {

                    // Create destination folder
                    boolean isCreated = destinationFolder.mkdir();
                    System.out.println("New directory is created");

                    if (isCreated) {
                        System.out.println("Destination directory is created at:: " + destinationFolder);
                    } else {
                        System.out.println("Some error occurred while creating destination directory");
                    }

                    // Get all the files from the sourceFolder
                    String[] files = sourceFolder.list();

                    // Iterate through all the files list and copy them one by one
                    for (String file : Objects.requireNonNull(files)) {

                        // Getting the source file
                        File sourceFile = new File(sourceFolder, file);

                        // Getting the destination file
                        File destinationFile = new File(destinationFolder, file);

                        // Recursive call so get all the sub directories
                        copyDirectory(sourceFile.getPath(), destinationFile.getPath());
                    }
                }
            } else {

                // Copy the files from one folder to another
                Files.copy(sourceFolder.toPath(), destinationFolder.toPath(), StandardCopyOption.REPLACE_EXISTING);

                System.out.println("File copied :: " + destinationFolder);
            }

            System.out.println("Copying process ends...");
        } catch (Exception e) {

            e.printStackTrace();
        }
    }

Here, we are taking source folder path and the destination folder path. There can be two cases here -

  • Source folder is a directory - in this case, we will first check if the destination folder exists or not. If it does not, then we will create it. After this, we will list all the files in the source folder and copy them one by one recursively.
  • Source folder is a file - In this case, we will directly create the file using the java.nio.file.Files. 

Deleting a directory recursively

Deleting a directory and all the content inside is a common use case in Java applications. In such a case we can follow below approach. 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
private static void deleteDirectory(String path){

        System.out.println("Recursive deletion starts...");

        try {

            // Getting the paths
            Path directory = Paths.get(path);

            Files.walkFileTree(directory, new SimpleFileVisitor<>(){

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes basicFileAttributes) throws IOException {

                    System.out.println("Deleting file :: " + file);

                    Files.delete(file);

                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException{

                    System.out.println("Deleting directory :: " + directory);

                    if(e == null){

                        Files.delete(directory);

                        return FileVisitResult.CONTINUE;
                    } else {
                        throw  e;
                    }
                }
            });

            System.out.println("Recursive deletion ends...");

        } catch (IOException e){

            e.printStackTrace();
        }
    }

Here, we are using Files.walkFileTree() method and SimpleFileVisitor to perform delete operation.

Java 8 provides a much cleaner approach to do this in very fewer lines of code as follows -

1
2
3
4
5
6
7
8
private static void deleteDirectory(String path) {
        Path dir = Paths.get(path);
 
        Files.walk(dir)
            .sorted(Comparator.reverseOrder())
            .map(Path::toFile)
            .forEach(File::delete);
    }

Conclusion

In this post, we discussed directory related operations using code samples.  

You can find the complete code on my GitHub repository.

I would love to hear your thoughts on this and would like have suggestions from you to make it better. 

Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Coding 😊

Comments

Popular posts from this blog

Threads in Java - Masterclass (Part 0)

Multithreading is a way to introduce concurrency in a program. In any case, if there are parallel paths in our program (parts which do not depend on the result from another part), we can make use of multithreading.
One should exploit this feature, especially with all these multiple core machines nowadays.

Below are a few reasons why we should use multithreading -
1. Keep a process responsive There was once a time when you would print a document in MS Word and the application would freeze for an annoyingly long amount of time until the job finished. Eventually, Microsoft solved this problem by running a printing job parallel to the main thread/ GUI thread.  To be clear though, not only GUI apps but Network services have to keep an ear to the ground for new clients, dropped connections and cancellation requests. In either case, it is critical to do the heavy lifting on a secondary thread to keep the user satisfied. 2. Keep a processor busy Keeping a processor busy can be a tough task e…

Parsing XML using Retrofit

Developing our own type-safe HTTP library to interface with a REST API can be a real pain as we have to handle many aspects -
making connectionscachingretrying failed requeststhreadingresponse parsingerror handling, and more.  Retrofit, on the other hand, is a well-planned, documented and tested library that will save you a lot of precious time and headaches. In this tutorial, we are going to discuss how we can parse the XML response returned from https://timesofindia.indiatimes.com/rssfeedstopstories.cms using the Retrofit library.

To work with Retrofit, we need three classes -  Model class to map the JSON dataInterfaces which defines the possible HTTP operationsRetrofit.Builder class - Instance which uses the interface and the Builder API which allows defining the URL endpoint for the HTTP operation. Every method of the above interface represents on possible API call. The request type is specified by using appropriate annotations (GET, POST). The response is returned as a Call object…

Material design profile page in Android

Hey everyone, some days back I was working on one my personal Android project. In that project, I was supposed to create a simple profile page for a user. This profile page was supposed to show some basic details of a user.

The output of this UI will be like this -
I created the profile page using material design and in this post, I am going to discuss a step by step tutorial to create a simple yet elegant profile page. Without further ado, let's get started.
Creating a new project Click on File ➤ New Project ➤ Empty Activity and fill the necessary details. Change styles.xml fileNavigate to app\src\main\res\values\styles.xmlChange the style value from DarkActionBar to NoActionBar as below<resources><!-- Base application theme. --><stylename="AppTheme"parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><itemname="colorPrimary">@color/colorPrimary</item><itemname="colorPrimaryDark&qu…