- Oct 30, 2000
- 14,665
- 440
- 126
I was trying to figure out a way to copy whole directories with files and subdirectories using java and all I could come up with is this copy class. Currently, the company I program for uses a file writing method of creating a batch file, based off the Operating System commands to do the copying. The problem is, that method isn't very operating system independant. Which is one of the reasons we are suppose to be usin java in the first place. I don't want to use an System.getProperty("osname") for figuring out how I'm going to code.
So anyhow, I write this nifty little recursive Copy class to do what I want instead. However, the lead "coder" and I use that term loosely because he is an EE and not a CS major guy, says my way would hog too many system resources since I use Filestreams. He says reading files in 2048 byte by 2048 byte would cause memory problems. I've done some testing with this so far and I've moved a few hundred megs in a couple of seconds with no problems and no corruptions. Anyhow, here's what I wrote.
import java.io.*;
public class Copy
{
/** Creates new Copy */
private Copy()
{
}
public static void copy(File destination, File source)
{
try
{
if (source.isDirectory())
{
if (!destination.isDirectory())
{
throw Exception e("Destination '"+destination.getName()+"' is not directory.");
}
copyDirectory(destination,source);
}
else
{
if (destination.isDirectory())
{
destination = new File(destination,source.getName());
}
copyFile(destination,source);
}
catch (Exception e)
{
}
}
protected static void copyDirectory(File destination, File source)
{
try
{
File[] list=source.listFiles();
for (int j=0;j<list.length;j++)
{
File dest = new File(destination,list[j].getName());
if (list[j].isDirectory())
{
dest.mkdir();
copyDirectory(dest,list[j]);
}
else
{
copyFile(dest,list[j]);
}
}
catch (Exception e)
{
}
}
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)
{
}
}
protected static void copyDirectoriesOnly(File destination, File source)
{
try
{
File[] list=source.listFiles();
for (int j=0;j<list.length;j++)
{
File dest = new File(destination,list[j].getName());
if (list[j].isDirectory())
{
dest.mkdir();
copyDirectoriesOnly(dest,list[j]);
}
}
}
catch (Exception e)
{
}
}
}//End of Copy Class
//(\___/)
//{=*.*=)
//(')__(')
Is there another way in java I could accomplish this part of my task without resorting to OS dependant commands by using java to copy files?
So anyhow, I write this nifty little recursive Copy class to do what I want instead. However, the lead "coder" and I use that term loosely because he is an EE and not a CS major guy, says my way would hog too many system resources since I use Filestreams. He says reading files in 2048 byte by 2048 byte would cause memory problems. I've done some testing with this so far and I've moved a few hundred megs in a couple of seconds with no problems and no corruptions. Anyhow, here's what I wrote.
import java.io.*;
public class Copy
{
/** Creates new Copy */
private Copy()
{
}
public static void copy(File destination, File source)
{
try
{
if (source.isDirectory())
{
if (!destination.isDirectory())
{
throw Exception e("Destination '"+destination.getName()+"' is not directory.");
}
copyDirectory(destination,source);
}
else
{
if (destination.isDirectory())
{
destination = new File(destination,source.getName());
}
copyFile(destination,source);
}
catch (Exception e)
{
}
}
protected static void copyDirectory(File destination, File source)
{
try
{
File[] list=source.listFiles();
for (int j=0;j<list.length;j++)
{
File dest = new File(destination,list[j].getName());
if (list[j].isDirectory())
{
dest.mkdir();
copyDirectory(dest,list[j]);
}
else
{
copyFile(dest,list[j]);
}
}
catch (Exception e)
{
}
}
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)
{
}
}
protected static void copyDirectoriesOnly(File destination, File source)
{
try
{
File[] list=source.listFiles();
for (int j=0;j<list.length;j++)
{
File dest = new File(destination,list[j].getName());
if (list[j].isDirectory())
{
dest.mkdir();
copyDirectoriesOnly(dest,list[j]);
}
}
}
catch (Exception e)
{
}
}
}//End of Copy Class
//(\___/)
//{=*.*=)
//(')__(')
Is there another way in java I could accomplish this part of my task without resorting to OS dependant commands by using java to copy files?