Reputation: 17893
I want to copy the directory structure without copying the content/files. I want to copy only folder structure. I have written a sample program but it is also copying the content/files also.
import java.io.*;
import java.nio.channels.*;
@SuppressWarnings("unused")
public class CopyDirectory{
public static void main(String[] args) throws IOException{
CopyDirectory cd = new CopyDirectory();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String source = "C:\\abcd\\Documents\\1";
File src = new File(source);
String destination = "C:\\abcd\\Documents\\2";
File dst = new File(destination);
cd.copyDirectory(src, dst);
}
public void copyDirectory(File srcPath, File dstPath) throws IOException{
if (srcPath.isDirectory())
{
if (!dstPath.exists())
{
dstPath.mkdir();
}
String files[] = srcPath.list();
for(int i = 0; i < files.length; i++)
{
System.out.println("\n"+files[i]);
copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
}
}
System.out.println("Directory copied.");
}
}
I am struck at this point. Thank you.
Upvotes: 0
Views: 3441
Reputation: 830
This worked for me:
import java.io.File;
public class StartCloneFolderOnly {
/**
* @param args
*/
public static void main(String[] args) {
cloneFolder("C:/source",
"C:/target");
}
public static void cloneFolder(String source, String target) {
File targetFile = new File(target);
if (!targetFile.exists()) {
targetFile.mkdir();
}
for (File f : new File(source).listFiles()) {
if (f.isDirectory()) {
String append = "/" + f.getName();
System.out.println("Creating '" + target + append + "': "
+ new File(target + append).mkdir());
cloneFolder(source + append, target + append);
}
}
}
}
Upvotes: 1
Reputation: 17893
import java.io.*;
import java.nio.channels.*;
@SuppressWarnings("unused")
public class CopyDirectory{
public static void main(String[] args) throws IOException{
CopyDirectory cd = new CopyDirectory();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String source = "C:\\abcd\\Documents\\1";
File src = new File(source);
String destination = "C:\\abcd\\Documents\\2";
File dst = new File(destination);
cd.copyDirectory(src, dst);
}
public void copyDirectory(File srcPath, File dstPath) throws IOException{
if (srcPath.isDirectory())
{
if (!dstPath.exists())
{
dstPath.mkdir();
}
String files[] = srcPath.list();
for(int i = 0; i < files.length; i++)
{
System.out.println("\n"+files[i]);
copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
}
}
System.out.println("Directory copied.");
}
}
Upvotes: 0
Reputation:
So if I'm right, you just want to copy the folders.
1.) Copy directory with sub-directories and files 2.) Place 1. wherever 3a.) Instantiate to list files in parent directory into an arrayList 3b.) Instantiate to list the new subfolders into an arrayList 3c.) Instantiate to list all files in each subfolder into their own arrayLists 4.) Use a for-loop to now delete every file within the new directory and subfolder
From this, you should have a copy of the new directory with all files removed.
Upvotes: 0