日期:2014-05-20 浏览次数:20761 次
**
* 复制文件或者目录
* @param srcFile:原始文件或目录
* @param desFile:目标文件或目录
*
*/
public static void copyFile(File srcFile, File desFile)throws Exception {
RandomAccessFile srcSaf = null;
RandomAccessFile desSaf = null;
try {
// 如果要复制的是文件夹,要先创建文件夹
if(srcFile.isDirectory()){
desFile.mkdir();
for(File sub : srcFile.listFiles()){
File newFile = new File(desFile, sub.getName());
copyFile(sub, newFile);
}
}
// 文件的复制
srcSaf = new RandomAccessFile(srcFile, "r");
desSaf = new RandomAccessFile(desFile, "rw");
byte[] buf = new byte[1024*10];
int len = 0;
while((len = srcSaf.read(buf)) != -1){
desSaf.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
throw e;
} finally{
srcSaf.close();
desSaf.close();
}
}
public static void copyFile(File srcFile, File desFile) {
RandomAccessFile srcSaf=null;
RandomAccessFile desSaf=null;
// 如果要复制的是文件夹,要先创建文件夹
if (srcFile.isDirectory()) {
desFile.mkdir();
for (File sub : srcFile.listFiles()) {
File newFile=new File(desFile,sub.getName());
copyFile(sub,newFile);
}
} else {
// 文件的复制
try {
srcSaf=new RandomAccessFile(srcFile,"r");
desSaf=new RandomAccessFile(desFile,"rw");
byte[] buf=new byte[1024*10];
int len=0;
while ((len=srcSaf.read(buf))!=-1) {
desSaf.write(buf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
srcSaf.close();
desSaf.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}