文件和文件目录重命名问题
来源:互联网 发布:企业画像 大数据 编辑:程序博客网 时间:2024/05/19 00:54
不多说,直接贴例子
/**
* 重命名文件名,新文件名不加后缀* @param filePath
* @param newName
*/
public static void rename(String filePath,String newName){
File file=new File(filePath);
if(!file.isDirectory()&&filePath.indexOf(".")!=-1){
String suffix=filePath.substring(filePath.lastIndexOf(".")+1,filePath.length());
newName=filePath.substring(0, filePath.lastIndexOf("/"))+newName+suffix;
}else{
//对目录进行处理
String str;
if(filePath.lastIndexOf("/")==filePath.length()-1){
str=filePath.substring(0, filePath.length()-1);
}else{
str=filePath;
}
//截取当前文件目录名
int index=str.lastIndexOf("/");
String fileName=str.substring(index+1,str.length());
//替换项目名为commonWeb的名字
newName=filePath.replace(fileName,newName);
}
file.renameTo(new File(newName));
}
这里的newName最终要是一个路径,而不是一个文件名,之前因为只写文件名,所以一直不成功。
0 0
- 文件和文件目录重命名问题
- C#中重命名文件和文件
- FCKeditor实现上传分月目录和文件重命名(C#)
- JAVA操作文件和文件目录
- Java文件对象创建目录和文件
- JAVA创建文件目录和文件
- java 创建文件和文件目录
- MediaScanner扫描文件和文件目录
- 目录下文件重命名
- 枚举目录和文件
- 目录和文件管理
- 访问目录和文件
- 目录和文件操作
- 目录和文件操作
- 复制目录和文件
- 列出指定文件的目录和文件,并进行缩进
- 流和文件(文件目录的一些操作)
- Android 重要的文件目录和文件分析
- Mysql开启远程连接方法
- UITextView
- 打包可执行程序,检查安装环境
- ajax 和 forword
- [分布式java]基于JavaAPI实现消息方式的系统间通信:TCP/IP+NIO
- 文件和文件目录重命名问题
- js各种框的总结
- 针对url参数的加密解密算法(原创)
- 【pushing my way】笛卡尔树
- 把txt文件转换成pdf格式的方法
- Treasure Hunt IV
- 这里讨论二个 PHP CURL CURLOPT 参数
- VMware虚拟机 ubuntu忘记密码解决方法
- Druid数据库连接池使用