个人认为递归调用dircopy()逻辑上有问题当File[] f1 = source.listFiles();
f1是空的情况(即文件夹里没有任何文件)
可能就有问题了?!

解决方案 »

  1.   

    try {
            // Create channel on the source
            FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();
        
            // Create channel on the destination
            FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();
        
            // Copy file contents from source to destination
            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        
            // Close the channels
            srcChannel.close();
            dstChannel.close();
        } catch (IOException e) {
        }
      

  2.   


    import java.io.File;
    import java.io.*;
    import java.util.zip.CRC32;
    public class CopyFile
    {
    private static final int nCopyBlockSize = 4096;
    private static String FileSeparatorSymbol =
    System.getProperty("file.separator");
    private FileInputStream inFileStream;
    private FileOutputStream outFileStream;

    public CopyFile()
    {
    inFileStream = null;
    outFileStream = null;
    }

    public boolean CopySingleFile(
    String srcFileFullName,
    String dstFileFullName)
    {
    boolean bRetCode = false;
    File srcFile = new File(srcFileFullName);
    File dstFile = new File(dstFileFullName);
    try
    {
    if (!srcFile.isFile())
    {
    return false;
    }
    if (!srcFile.canRead())
    {
    return false;
    } if (!dstFile.exists() && !dstFile.createNewFile())
    return false; if (!dstFile.canWrite())
    {
    return false;
    }
    }
    catch (IOException e)
    {
    return false;
    }
    try
    {
    inFileStream = new FileInputStream(srcFile);
    outFileStream = new FileOutputStream(dstFile);
    }
    catch (FileNotFoundException e)
    {
    return false;
    }
    byte bCopyFileBuffer[] = new byte[4096];
    int value = -1;
    try
    {
    while ((value = inFileStream.read(bCopyFileBuffer)) != -1)
    outFileStream.write(bCopyFileBuffer, 0, value);
    bRetCode = true;
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    try
    {
    if (inFileStream != null)
    inFileStream.close();
    if (outFileStream != null)
    outFileStream.close();
    }
    catch (IOException e)
    {
    }
    return bRetCode;
    } /*
     * Copy a single file with crc value check, if copy failed or crc check failed,
     * return false, else if sucess return true.
     * 
     */
    public boolean CopySingleFile(
    String srcFileFullName,
    String dstFileFullName,
    long lCRCValue)
    {
    boolean bRetCode = false;
    File srcFile = new File(srcFileFullName);
    File dstFile = new File(dstFileFullName);
    try
    {
    if (!srcFile.isFile())
    {
    return false;
    }
    if (!srcFile.canRead())
    {
    return false;
    }
    if (!dstFile.exists() && !dstFile.createNewFile())
    return false;
    if (!dstFile.canWrite())
    {
    return false;
    }
    }
    catch (IOException e)
    {
    return false;
    }
    try
    {
    inFileStream = new FileInputStream(srcFile);
    outFileStream = new FileOutputStream(dstFile);
    }
    catch (FileNotFoundException e)
    {
    return false;
    }
    byte bCopyFileBuffer[] = new byte[4096];
    CRC32 crcChecker = new CRC32();
    int value = -1;
    try
    {
    while ((value = inFileStream.read(bCopyFileBuffer)) != -1)
    {
    outFileStream.write(bCopyFileBuffer, 0, value);
    crcChecker.update(bCopyFileBuffer, 0, value);
    }
    if (lCRCValue == 9999L || crcChecker.getValue() == lCRCValue)
    bRetCode = true;
    }
    catch (IOException e)
    {
    }
    try
    {
    if (inFileStream != null)
    inFileStream.close();
    if (outFileStream != null)
    outFileStream.close();
    }
    catch (IOException e)
    {
    }
    return bRetCode;
    }

    /*
     * src and dst must be directory.
     * 
     */
    public boolean CopyPackage(String src, String dst)
    {
    if (src == null || dst == null)
    return false;

    int nRetCode = 0;
    File sourceDir = new File(src);

    //ネ郢皛rcイサエ贇レサ゚イサハヌトソツシ」ャキオサリfalse
    if (!sourceDir.exists() || !sourceDir.isDirectory())
    return false;
    String list[] = sourceDir.list();

    if (list == null)
    return false;

    //ナミカマトソア�トソツシ
    File dstDir = new File(dst);
    if (!dstDir.exists() && !dstDir.mkdirs())
    {
    return false;
    }
    else if (!dstDir.isDirectory())
    return false; if (!src.endsWith(File.separator))
    src = src + File.separator; if (!dst.endsWith(File.separator))
    dst = dst + File.separator; for (int i = 0; i < list.length; i++)
    {
    File tmp = new File(src + list[i]);
    if (tmp.isDirectory())
    {
    if (!CopyPackage(src + list[i], dst + list[i]))
    return false;
    }
    else
    {
    CopySingleFile(src + list[i], dst + list[i]);
    }
    }
    return true;
    }
    }
      

  3.   

    snap2008cn(清忍诺夫) :果然是你说的问题,多谢。zhengkan:也感谢你的代码。