public class TestCopy
{
public static void main(String[] args)
{
TestCopy test = new TestCopy();
test.copyTo(new StringBuffer("f:/books"), new StringBuffer("f:/booksCOPY"));
}
public void copyTo (StringBuffer s_path, StringBuffer t_path)
{
File s_file = new File(s_path.toString());
File t_file = new File(t_path.toString());
if (!t_file.exists())
t_file.mkdir();
File[] files = s_file.listFiles();
for (int i = 0; i < files.length; i++)
{
System.out.println(files[i].getName());
if (files[i].isDirectory())
{
StringBuffer s_subPath = new StringBuffer(s_path.toString());
StringBuffer t_subPath = new StringBuffer(t_path.toString());
t_subPath.append("/");
s_subPath.append("/");
t_subPath.append(files[i].getName());
s_subPath.append(files[i].getName());
File subDir = new File(t_subPath.toString());
if (subDir.mkdir())
copyTo(s_subPath, t_subPath);
}
else if (files[i].isFile())
{
File t_subFile = new File(t_path.toString() + "/" + files[i].getName());
File s_subFile = new File(s_path.toString() + "/" + files[i].getName());
try
{
FileInputStream fin = new FileInputStream(files[i]);
FileOutputStream fout = new FileOutputStream(t_subFile);
int length;
while ((length = fin.read()) != -1)
{
byte[] buffer = new byte[1024];
fin.read(buffer, 0, length);
fout.write(buffer, 0, length);
}
}
catch (IOException ioe)
{
}
catch (Exception e)
{
}