/**
* 文件、文件夹重命名
* @param newFileName 新文件名如有后缀名,带后缀名 例如"低价.js"
* @param filepath 文件绝对路径 例如 "E:/测试/202103/新建文件夹 (2)/新建文件夹/你好.js"
* @return 返回值为true则成功,false失败
*/
public boolean rename(String newFileName,String filepath){
File file =new File(filepath.substring(0,filepath.lastIndexOf("/")+1)+newFileName);
return new File(filepath).renameTo(file);
}
/**
* 剪切
* @param newFilePath 转移目标路径不带文件名 例如"D:/新建文件夹"
* @param filepath 原始路径带文件名 例如 "E:/测试/202103/新建文件夹 (2)/新建文件夹/你好.js"
* @return 返回值为true则成功,false失败
*/
public boolean remove(String newFilePath,String filepath){
int i = filepath.lastIndexOf("/")+1;
String Name = filepath.substring(i);
newFilePath+="/"+Name;
File file =new File(newFilePath);
return new File(filepath).renameTo(file);
}
/**
* 创建新文件夹
* @param folderPath 文件夹路径 例如“"E:/测试/202103"”
* @return 返回值为true则成功,false失败或者该文件夹已存在
*/
public boolean creatFolder(String folderPath){
File file = new File(folderPath);
if (!file.exists() && !file.isDirectory()){
return file.mkdirs();
}else {
return false;
}
}
java中file给文件重命名、剪切文件到指定位置、创建文件夹
最新推荐文章于 2024-05-06 18:26:10 发布