Apache Commons IO to construct File from Directory and Name Elements using FileUtils

Tags: Apache Commons IO

Java Code Examples for using getFile() method of org.apache.commons.io.FileUtils class

Example 1

package simplesolution.dev;

import org.apache.commons.io.FileUtils;
import java.io.File;

public class FileUtilsGetFileExample1 {

    public static void main(String... args) {
        // construct File from name elements
        File file = FileUtils.getFile("D:", "Data", "file1.txt");

        System.out.println("Absolute file path: " + file.getAbsolutePath());
        System.out.println("File exist: " + file.exists());
    }
}

Example 2

package simplesolution.dev;

import org.apache.commons.io.FileUtils;
import java.io.File;

public class FileUtilsGetFileExample2 {

    public static void main(String... args) {
        File directory = new File("D:\\");
        // construct File from name elements
        File file = FileUtils.getFile(directory, "Data", "file1.txt");

        System.out.println("Absolute file path: " + file.getAbsolutePath());
        System.out.println("File exist: " + file.exists());
    }
}
Happy Coding 😊

Apache Commons IO to read text file content as a list of Strings

Apache Commons IO with LineIterator to read file content

Apache Commons IO to read text content from Reader

Apache Commons IO to read text content from InputStream