Java Coding Problem

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
I'm writing an export function for some code I have but I'm a bit stumped on this. I'm trying to copy a directory, or at least the entire contents including sub directories from one location to another. I already got the full paths from location A to location B using JFileChooser. The next part is now copying stuff over. The problem is I can only find a way to do this one file at a time. then again, I could be having a brain fart and not thinking about this the right way.

Anyone have an idea?
 

HumblePie

Lifer
Oct 30, 2000
14,665
440
126
nvm brain fart just wrote this bit up

protected static void copyDirectory(File destination, File source)
{
File[] list=source.listFiles();
for (int i=0;i<list.length;i++)
{
File dest = new File(destination,list.getName());
if (list.isDirectory())
{
dest.mkdir();
copyDirectory(dest,list);
}
else
{
copyFile(dest,list);
}
}
}

protected static void copyFile(File destination, File source)
{
try
{
FileInputStream inStream = new FileInputStream(source);
FileOutputStream outStream = new FileOutputStream(destination);

int len;
byte[] buf=new byte[2048];

while ((len=inStream.read(buf))!=-1)
{
outStream.write(buf,0,len);
}
}
catch (Exception e)
{
throw new CopyException("Can't copy file "+source+" -> "+destination+".",e);
}
}