0

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\
Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
prog rice bowl
  • 788
  • 3
  • 19
  • 36

3 Answers3

1

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");
}
Prasad
  • 1,188
  • 3
  • 11
  • 29
1

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);
Salah
  • 8,567
  • 3
  • 26
  • 43
1

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/

Sandy
  • 226
  • 2
  • 4