java实现文件名以文件夹名开头
程序员文章站
2022-04-17 11:41:13
...
实现效果:
Java代码:
package com.binchi.test;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test extends JFrame {
JTextField textFile;
int m;
public static void main(String[] args) {
Test test=new Test();
}
public Test() {
this.setTitle("更换文件名");
this.add(mainPanel());
this.setBounds(500, 200, 500, 100);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public JPanel mainPanel() {
JPanel panel = new JPanel();
JButton btn=new JButton("OK");
textFile = new JTextField("输入目录,支持修改png|jpg|jpeg|gif格式", 30);
panel.add(textFile);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String nFilePath;
m=0;
if(textFile.getText().trim().contains("/")||textFile.getText().trim().contains("\\")) {
nFilePath=textFile.getText().trim();
textFile.setText("正在寻找路径");
}else {
nFilePath="./测试";
textFile.setText("检查路径");
}
changeFileName(nFilePath);
}
});
panel.add(btn);
return panel;
}
private void changeFileName(String filePath_first){
String[] folderSize = new String[5];
String folder = null;
File file_second = null;
File fileFolder = null;
File absPath;
File replaceAbsFile = null;
String replaceReName;
String replaceAbsName;
String oldAbsName = null;
String fileName = null;
List<String> fileFolderList = new ArrayList<String>();
List<String> unUseFolderList = new ArrayList<String>();
if(filePath_first=="./测试") {//生成文件夹 测试用
File fileDir = new File(filePath_first);
if (!fileDir.exists()) {
fileDir.mkdir();
}
textFile.setText("请检查路径,建立测试文件夹");
for (int i = 0; i < folderSize.length; i++) {
folder = 2019071800 + i + "_";
fileFolder = new File(filePath_first + "/" + folder);
if (!fileFolder.exists()) {
fileFolder.mkdir();
fileFolderList.add(fileFolder.getAbsolutePath());
} else {
fileFolderList.add(fileFolder.getAbsolutePath());
// System.out.println(fileFolder.getAbsolutePath());
}
}
}else {//正常使用
fileFolder = new File(filePath_first);
if (fileFolder.exists()) {
for (int i = 0; i < fileFolder.listFiles().length; i++) {
fileFolderList.add(fileFolder.listFiles()[i].getAbsolutePath());
}
}else {
textFile.setText("路径不存在");
return;
}
}
//便利文件夹
int k = 1;
for (int i = 0; i < fileFolderList.size(); i++) {
file_second = new File(fileFolderList.get(i));
//if(file_second.isDirectory()) {
//文件夹下文件
if(file_second.isDirectory()) {//如果是目录
//获取目录下所有子文件和子目录
File[] file = file_second.listFiles();
//遍历所有目录和文件
int d=1;
for (int j = 0; j < file.length; j++) {
if(file[j].isFile()) {//如果是文件
oldAbsName = file_second.getAbsolutePath() + "\\" + file[j].getName();
System.out.println("是文件"+oldAbsName);
if (file[j].getName().endsWith("png")||file[j].getName().endsWith("jpg")||file[j].getName().endsWith("jpeg")||file[j].getName().endsWith("gif")) {
System.out.println("原文件绝对路径"+oldAbsName);
absPath = new File(oldAbsName);
replaceReName = fileFolderList.get(i).substring(fileFolderList.get(i).lastIndexOf("\\") + 1,fileFolderList.get(i).length());
fileName=file[j].getName().substring(file[j].getName().lastIndexOf(".")+1, file[j].getName().length());
replaceAbsName = fileFolderList.get(i) + "\\" + replaceReName +d+++"."+fileName;
System.out.println("替换文件的绝对路径"+replaceAbsName);
replaceAbsFile=new File(replaceAbsName);
if(absPath.compareTo(replaceAbsFile)!=0) {
if (absPath.renameTo(replaceAbsFile)) {
m++;
System.out.println("修改文件名成功");
textFile.setText("当前至"+oldAbsName+","+m+"个文件修改成功");
}
}
}
}else if(file[j].isDirectory()) {//如果是目录 就 递归找文件
System.out.println("是目录"+file_second.getAbsolutePath());
unUseFolderList.add(file_second.getAbsolutePath());
}
}
}else if(file_second.isFile()){//如果是文件
if (file_second.getName().endsWith("png")||file_second.getName().endsWith("jpg")||file_second.getName().endsWith("jpeg")||file_second.getName().endsWith("gif")) {
String parentPath=file_second.getParent();
folder=file_second.getParent().substring(parentPath.lastIndexOf("\\")+1, parentPath.length());
oldAbsName=file_second.getAbsolutePath();
System.out.println("原文件绝对路径"+oldAbsName);
fileName=file_second.getName().substring(file_second.getName().lastIndexOf(".")+1, file_second.getName().length());
replaceAbsName = parentPath + "\\" + folder +k+++"."+fileName;
System.out.println("替换文件的绝对路径"+replaceAbsName);
replaceAbsFile=new File(replaceAbsName);
absPath = new File(oldAbsName);
if(absPath.compareTo(replaceAbsFile)!=0) {
if (absPath.renameTo(replaceAbsFile)) {
m++;
System.out.println("修改文件名成功");
textFile.setText("当前至"+oldAbsName+","+m+"个文件修改成功");
}
}
}
}
}
if(!unUseFolderList.isEmpty()) {
for (int j = 0; j < unUseFolderList.size(); j++) {
System.out.println("目录");
changeFileName(unUseFolderList.get(j));
}
}
}
}