My linux world » Java – Files and Folders

Java - Files and Folders


Contents

Dependencies

If you use maven, add this to your pom.xml:

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>${commonsio.version}</version>
</dependency>

Copy directory

org.apache.commons.io.FileUtils.copyDirectory(srcDir, destDir);

Copy directory using regular expression as filter

FileFilter filter = element -> element.getName().matches(regex);
 
org.apache.commons.io.FileUtils.copyDirectory(srcDir, destDir, filter );

Move directory using regular expression as filter

FileFilter filter = element -> element.getName().matches(regex);
 
File[] fileList = srcDirectory.listFiles(filter);
 
for(File file : fileList ) {
  file.renameTo(new File(destDir, file.getName()));
}

Delete directory

org.apache.commons.io.FileUtils.deleteDirectory(srcDir, destDir);

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.