文件移动以及重命名

package com.coding;

import java.io.File;

public class Test23 {

public static void main(String[] args) {

String[] fileNameArr = {"01.jpeg", "02.jpeg", "03.jpeg", "04.jpeg", "05.jpeg"};

Test23 test23 = new Test23();
for (int i = 0; i < fileNameArr.length; i++) {
test23.moveFile(fileNameArr[i]);
}

}

private void moveFile(String fileName) {

String oldFilePath = "D:\\test\\move01\\";
String newFilePath = "D:\\test\\move02\\";

File oldFile = new File(oldFilePath + fileName);

String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
String fileNameStr = fileName.substring(0, fileName.lastIndexOf("."));
File newFile = new File(newFilePath + fileNameStr + "aaa" + fileType);

oldFile.renameTo(newFile);

}
}

posted on 2023-02-18 10:30  ~码铃薯~  阅读(22)  评论(0编辑  收藏  举报

导航