最简单的就是递归了
代码是将一个目录下的所有文件拷贝到另一个目录下
import java.io.*;
import java.util.*;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)
{
}

}
}
}
}

解决方案 »

  1.   


    给点分吧  private boolean Browse(String path, String filespec) {
        //m_fInit = new File(path);
        this.cd(path);
        //首先查找dir中符合要求的文件
         DirListInfo[] m_listInfo=showFileContents(new SearchFilter(filespec));
        //File[] files = m_fInit.listFiles(new SearchFileFilter(filespec));
        if (m_listInfo != null && m_listInfo.length > 0) {
          for (int i = 0; i < m_listInfo.length; i++) {
            //检查是不是目录
            //如果不是,则进行处理
            if (m_listInfo[i].isFile()) {
              String filename = m_listInfo[i].m_sName;
              if (!processFile(filename))
                return false;
            }
            else {        }
          }
        }
        //查找dir中的子目录
        //因为在处理dir中的文件时,派生类的ProcessFile有可能改变了
        //当前目录,因此还要重新设置当前目录为dir。
        //执行过_findfirst后,可能系统记录下了相关信息,因此改变目录
        //对_findnext没有影响。    this.cd(path);
        //首先查找dir中符合要求的文件
        m_listInfo=showFileContents(null);
        if (m_listInfo != null && m_listInfo.length > 0) {
          for (int i = 0; i < m_listInfo.length; i++) {
            if (m_listInfo[i].isDir()) {
              //System.out.println(m_listInfo[i].m_sName+",,,"+m_listInfo[i].m_sName.endsWith(".")+"    "+m_listInfo[i].m_sName.endsWith(".."));
              if (!m_listInfo[i].m_sName.endsWith("..") &&
                  !m_listInfo[i].m_sName.endsWith(".")) {
                String subPath = m_listInfo[i].m_sName+"/";
                processDir(subPath, path);
                if (!Browse(subPath, filespec))
                  return false;
              }
            }
          }
        }
        return true;
      }  private boolean processFile(String fileName) {
        System.out.println("file:"+getPath()+fileName);
        return true;
      }  private boolean processDir(String currentDir, String parentDir) {
        //System.out.println("Dir::"+currentDir);
        return false;
      }
      

  2.   

    来晚了,呵呵!!不过下面的我想正是楼主想要的:http://www.chinajavaworld.com/bbsoffline/jinghuaforum6/33.html----------------------------------------------------------------
    看完了别忘了给信息费哦,呵呵