prog rice bowl
prog rice bowl

Reputation: 778

How to create a directory using File.getParentFile().getName() with relative path

I am having an input folder say c:\files\input\ that contains my list of files that I am using.

How do I use the above to create say c:\files\output\ and copy the files from the input folder to the output folder?

My c:\files\input is read from an object, say

String inputFolder = dataMap.getString("folder");// this will get c:\files\input\

Upvotes: 0

Views: 630

Answers (3)

Sandy
Sandy

Reputation: 226

To Create the directory you can refer to the below code

File file = new File("c:\\files\\output");
        if (!file.exists()) {
            if (file.mkdir()) {
                System.out.println("Directory is created!");
            } else {
                System.out.println("Failed to create directory!");
            }
        }

To copy files from a directory to another directory.. refer to the following link it gives a good explanation with source code examples

http://examples.javacodegeeks.com/core-java/io/file/4-ways-to-copy-file-in-java/

Upvotes: 1

Salah
Salah

Reputation: 8657

You can use FileUtils from org.apache.commons.io library

FileUtils.copyDirectory(srcDir, destDir);

so in your case:

File file = new File(inputFolder);
String parentDir = file.getParentFile().getAbsolutePath();
File outputDir = new File(parentDir, "output");
if(!outputDir.exsit()) {
    outputDir.mkdir();
}
FileUtils.copyDirectory(inputFolder, outputDir);

Upvotes: 1

Prasad
Prasad

Reputation: 1188

You got path of folder in variable inputFolder now do as follows.

String inputFolder = dataMap.getString("folder");

File dir = new File(inputFolder);
if(dir.mkdirs()){
    System.out.println("Directory created");
}else{
    System.out.println("Directory Not Created");
}

Upvotes: 1

Related Questions