Java中怎么移动文件_使用java移动文件
在使用java来移动文件时,主要利用IO中的File类,在File类中可以利用renameTo方法实现文件的移动,实例如下:
private boolean RemoveFile(String fileName,String destinationFloderUrl)
{
File file = new File(fileName);
File destFloder = new File(destinationFloderUrl);
//检查目标路径是否合法
if(destFloder.exists())
{
if(destFloder.isFile())
{
this.logger.error("目标路径是个文件,请检查目标路径!");
return false;
}
}else
{
if(!destFloder.mkdirs())
{
this.logger.error("目标文件夹不存在,创建失败!");
return false;
}
}
//检查源文件是否合法
if(file.isFile() &&file.exists())
{
String destinationFile = destinationFloderUrl+"/"+file.getName();
if(!file.renameTo(new File(destinationFile)))
{
this.logger.error("移动文件失败!");
return false;
}
}else
{
this.logger.error("要备份的文件路径不正确,移动失败!");
return false;
}
this.logger.info("已成功移动文件"+file.getName()+"到"+destinationFloderUrl);
return true;
}
在使用java来移动文件时,主要利用IO中的File类,在File类中可以利用renameTo方法实现文件的移动,实例如下: private boolean RemoveFile(String fileName,String destinationFloderUrl) { File file = new File(fileName); File destFloder = new File(destinationFloderUrl); //检查目标路径是否合法 if(destFloder.exists()) { if(destFloder.isFile()) { this.logger.error("目标路径是个文件,请检查目标路径!"); return false; } }else { if(!destFloder.mkdirs()) { this.logger.error("目标文件夹不存在,创建失败!"); return false; } } //检查源文件是否合法 if(file.isFile() &&file.exists()) { String destinationFile = destinationFloderUrl+"/"+file.getName(); if(!file.renameTo(new File(destinationFile))) { this.logger.error("移动文件失败!"); return false; } }else { this.logger.error("要备份的文件路径不正确,移动失败!"); return false; } this.logger.info("已成功移动文件"+file.getName()+"到"+destinationFloderUrl); return true; }