请大家帮忙看看这段copy程序 个人认为递归调用dircopy()逻辑上有问题当File[] f1 = source.listFiles();f1是空的情况(即文件夹里没有任何文件)可能就有问题了?! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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) { } 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; }} snap2008cn(清忍诺夫) :果然是你说的问题,多谢。zhengkan:也感谢你的代码。 这题目……好像理解错了…… 请问如何在JTable中添加和修改一行数据??谢谢 求学 JavaBean里的getXXX函数是否可以带参数? 一个窗体的问题 关与递归调用的疑问 JAVA中如何将工具栏中的按钮变成透明的???????????? 大家开发java+sql2000用什么连接池? 祝所有大连的 csdn 中 java 版的 网友圣诞快乐!!!!:) JBuilder 3.5 需要 serial number 与 key请大家给我一两个 ★100分★关于键盘事件处理的三个问题★★ synchronized放在哪里?
// 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) {
}
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;
}
}