this.copyFile(
source_name+"/"+fileList[i].getName(),
dest_name+"/"+fileList[i].getName(),
type);
}
else if(fileList[i].isDirectory()){
//弓拷
copyDirectory(
source_name+"/"+fileList[i].getName(),
dest_name+"/"+fileList[i].getName(), type);
}
}
result = 1;
}
return result;
}
}
用java写的一个文件操作类包(4)
时间:2011-02-05 javaeye wakin2003
移动:本来想用renameTo方法来实现的,但是发现这个方法有些问题。在我的博客里写明了,希望大家能指点一下。
Java代码
package fileOperation;
import java.io.File;
import java.io.IOException;
/**
* 实现移动文件的功能,利用delete类和copy类来实现。
* @author wakin
*
*/
public class Move {
/**
* 利用copy类的函数和delete类来完成move的操作。
* @param source_name
* @param dest_name
* @param type
* @return
* @throws IOException
*/
public int move(String source_name,String dest_name,int type) throws IOException{
int result = 0;
Copy copy = new Copy();
Delete delete = new Delete();
File source_file = new File(source_name);
//File dest_file = new File(dest_name);
if(!source_file.exists())
throw new RuntimeException("FileMove: no such source file:"+source_name);
if(source_file.isFile()){
result = copy.copyFile(source_name, dest_name, type); //调用Copy类的copyFile函数
if(result ==1)
delete.deleteFile(source_name); //调用Delete类的deleteFile函数删除源文件
}
else {
result = copy.copyDirectory(source_name, dest_name, type); //调用Copy类的copyDirectory函数
if(result == 1)
delete.deleteDir(source_name); //调用Delete类的deleteDir函数删除源目录
}
return result;
}
}
package fileOperation;
import java.io.File;
import java.io.IOException;
/**
* 实现移动文件的功能,利用delete类和copy类来实现。
* @author wakin
*
*/
public class Move {
/**
* 利用copy类的函数和delete类来完成move的操作。
* @param source_name
* @param dest_name
* @param type
* @return
* @throws IOException
*/
public int move(String source_name,String dest_name,int type) throws IOException{
int result = 0;
Copy copy = new Copy();
Delete delete = new Delete();
File source_file = new File(source_name);
//File dest_file = new File(dest_name);
if(!source_file.exists())
throw new RuntimeException("FileMove: no such source file:"+source_name);
if(source_file.isFile()){
result = copy.copyFile(source_name, dest_name, type); //调用Copy类的copyFile函数
if(result ==1)
delete.deleteFile(source_name); //调用Delete类的deleteFile函数删除源文件
}
else {
result = copy.copyDirectory(source_name, dest_name, type); //调用Copy类的copyDirectory函数
if(result == 1)
delete.deleteDir(source_name); //调用Delete类的deleteDir函数删除源目录
|