Java Create File from Directory and Name Elements using Apache Commons IO
Tags: FileUtils Apache Commons Apache Commons IO File
In this Java tutorial we learn how to construct a new File from a given directory and names elements such as sub directories name, file name using FileUtils class of Apache Commons IO library.
How to add Apache Commons IO library to your Java project
To use the Apache Commons IO library in the Gradle build project, add the following dependency into the build.gradle file.
implementation 'commons-io:commons-io:2.8.0'
To use the Apache Commons IO library in the Maven build project, add the following dependency into the pom.xml file.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
To have more information about the Apache Commons IO library you can visit the library home page at commons.apache.org/proper/commons-io/
Create File object from given directory and name elements
The following example code shows how to construct a File from a given directory, sub directories names and a file name.
ConstructFileFromNames1.java
import org.apache.commons.io.FileUtils;
import java.io.File;
public class ConstructFileFromNames1 {
public static void main(String... args) {
File directory = new File("D:\\Data");
File file = FileUtils.getFile(directory, "subFolder", "data.txt");
System.out.println("The file path is: " + file.getAbsolutePath());
}
}
The file path is: D:\Data\subFolder\data.txt
Create File object from name elements
The following example code shows how to construct a File from given sub directories names and a file name.
ConstructFileFromNames2.java
import org.apache.commons.io.FileUtils;
import java.io.File;
public class ConstructFileFromNames2 {
public static void main(String... args) {
File file = FileUtils.getFile("D:\\Data", "subFolder", "data.txt");
System.out.println("The file path is: " + file.getAbsolutePath());
}
}
The file path is: D:\Data\subFolder\data.txt
Create Directory object from given directory and name elements
The following example code shows how to construct a directory from a given directory, sub directories names.
ConstructDirectoryFromNames1.java
import org.apache.commons.io.FileUtils;
import java.io.File;
public class ConstructDirectoryFromNames1 {
public static void main(String... args) {
File directory = new File("D:\\Data");
File file = FileUtils.getFile(directory, "subFolder", "a", "b", "c");
System.out.println("The directory path is: " + file.getAbsolutePath());
}
}
The directory path is: D:\Data\subFolder\a\b\c
Create Directory object from name elements
The following example code shows how to construct a directory from given sub directories names.
ConstructDirectoryFromNames2.java
import org.apache.commons.io.FileUtils;
import java.io.File;
public class ConstructDirectoryFromNames2 {
public static void main(String... args) {
File file = FileUtils.getFile("D:\\Data", "subFolder", "a", "b", "c");
System.out.println("The directory path is: " + file.getAbsolutePath());
}
}
The directory path is: D:\Data\subFolder\a\b\c
Happy Coding 😊