在java中使用 File.renameTo(File)实现重命名.

Here is part of my files:网址:yii666.com<网址:yii666.com

 [北京圣思园Java培训教学视频]Java.SE.前9日学习成果测试题(2010年12月2日).rar
[北京圣思园Java培训教学视频]Java.SE.第一百一十一讲.基于UDP的网络通信详解.rar
[北京圣思园Java培训教学视频]Java.SE.第一百一十七讲.Java.SE项目迭代二深度详解之线程对象设计.rar
[北京圣思园Java培训教学视频]Java.SE.第一百一十三讲.Java.SE项目迭代一.rar
[北京圣思园Java培训教学视频]Java.SE.第一百一十九讲.Java.SE项目迭代二深度详解之系统交互.rar
[北京圣思园Java培训教学视频]Java.SE.第一百一十二讲.基于UDP的网络通信详解.续.rar
[北京圣思园Java培训教学视频]Java.SE.第一百一十五讲.Java.SE项目迭代一精讲.续.rar
As you can image,if I gonna learn the courses course-to-course.I gonna find it difficult and boring to find each them,and the sort type is not what we want. So I have to rename the file as simple as possible.

Here is just  a segment code that demonstates how to use the  File.renameTo(File).文章来源地址https://www.yii666.com/article/764143.html文章地址https://www.yii666.com/article/764143.html文章来源地址:https://www.yii666.com/article/764143.html

 public static void main(String[] args) {
String path = "H:\\Java\\JavaSE\\";
File root = new File(path);
String oriName = "";
String newName = "";
String toClip = "[北京圣思园Java培训教学视频]Java.SE.";
for (String f : root.list()) {
File fSub = new File(path + f); //File to be rename(Must append from the root path).
if (!fSub.isFile()) //Only operate File.
continue;
if (fSub.getName().startsWith(toClip)) {
oriName =fSub.getName();
newName = oriName.substring(toClip.length());
File fSubNew = new File(path + newName); //The target File.
if(fSub.renameTo(fSubNew))
System.out.println(fSub.getAbsolutePath() + " rename successfully");
else
System.out.println(fSub.getAbsolutePath() + " rename failed");
}
}
}

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png