比如平常网站下载的教学视频由于文件名字过于复杂啰嗦,所以有时可能需要进行文件的重命名操作
比如:
进行重命名操作:
package my;
import java.io.File;
public class ChangeFileName
{
public static void main(String[] args)
{
File f = new File("E:\\尚硅谷-数据结构+算法");
File[] files = f.listFiles();
for(File oldFile : files)
{
String fileName = oldFile.getName(); //旧名
String newName = oldFile.getName().substring(0, 3) + '.' + oldFile.getName().substring(24);//新名
File newFile = new File("E:\\尚硅谷-数据结构+算法", newName);
System.out.println(fileName + "-->" + newName);
//进行重命名操作
if(oldFile.renameTo(newFile))
{
System.out.println(oldFile.getName().substring(0, 3) + "重命名成功---");
}
else
{
System.out.println(oldFile.getName().substring(0, 3) + "重命名失败+++");
}
}
}
}
重命名之后: