How to copy directory in Java

In Java, we can use the Java 1.7 FileVisitor or Apache Commons IO FileUtils.copyDirectory to copy a directory, which includes its sub-directories and files. This article shows a few of the common ways to copy a directory in Java. FileVisitor (Java 7+) FileUtils.copyDirectory (Apache commons-io) Custom Copy using Java 7 NIO and Java 8 Stream. …

Read more

How to check if directory is empty in Java

Here’s an example to check if a directory is empty. Example package com.mkyong.file; import java.io.File; public class CheckEmptyDirectoryExample { public static void main(String[] args) { File file = new File("C:\\folder"); if(file.isDirectory()){ if(file.list().length>0){ System.out.println("Directory is not empty!"); }else{ System.out.println("Directory is empty!"); } }else{ System.out.println("This is not a directory"); } } }

How to delete directory in Java

If we use the NIO Files.delete to delete a non-empty directory in Java, it throws DirectoryNotEmptyException; for legacy IO File.delete to delete a non-empty directory, it returns a false. The standard solution is to loop the directory recursively, and delete all its children’s contents first (sub-files or sub-directories), and delete the parent later. This example …

Read more

How to create directory in Java

In Java, we can use the NIO Files.createDirectory to create a directory or Files.createDirectories to create a directory including all nonexistent parent directories. try { Path path = Paths.get("/home/mkyong/a/b/c/"); //java.nio.file.Files; Files.createDirectories(path); System.out.println("Directory is created!"); } catch (IOException e) { System.err.println("Failed to create directory!" + e.getMessage()); } 1. Create Directory – Java NIO 1.1 We can …

Read more

How to traverse a directory structure in Java

In this example, the program will traverse the given directory and print out all the directories and files absolute path and name one by one. Example package com.mkyong.io; import java.io.File; public class DisplayDirectoryAndFile{ public static void main (String args[]) { displayIt(new File("C:\\Downloads")); } public static void displayIt(File node){ System.out.println(node.getAbsoluteFile()); if(node.isDirectory()){ String[] subNote = node.list(); for(String …

Read more

How to get the current working directory in Java

In Java, we can use System.getProperty("user.dir") to get the current working directory, the directory from where your program was launched. String dir = System.getProperty("user.dir"); // directory from where the program was launched // e.g /home/mkyong/projects/core-java/java-io System.out.println(dir); One of the good things about this system property user.dir is we can easily override the system property via …

Read more

How to check if a file exists in Java

In Java, we can use Files.exists(path) to test whether a file exists. The path can be a file or a directory. It is better to combine with !Files.isDirectory(path) to ensure the existing file is not a directory. Path path = Paths.get("/home/mkyong/test/test.log"); // file exists and it is not a directory if(Files.exists(path) && !Files.isDirectory(path)) { System.out.println("File …

Read more

How to copy Entire Directory in Linux

Command is simple, here i provide two samples to show how to copy entire directory in linux. cp -r sourcedir targetdir for instance, 1) Copy anything from current directory to /usr/local/download cp -r * /usr/local/download 2) Copy whole directory (include content) /usr/local/fromdownload to target /usr/local/download cp -r /usr/local/fromdownload /usr/local/download