美文网首页
批量修改文件名小工具

批量修改文件名小工具

作者: 勇者与王者 | 来源:发表于2019-10-15 17:26 被阅读0次
package javaApplication;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
* 
* @author qz:
* @version 创建时间:2019年10月15日 上午11:14:46
* 
* 读取用户输入的目录、路径,批量修改目录下文件
* 用户可以自行输入前缀和文件名分隔符和后缀
*/
public class RenameBatch {

    

    public static void main(String[] args)  {
        // TODO Auto-generated method stub

        System.out.println("Enter the folder to rename: ");
        Scanner in = new Scanner(System.in);
        String path = in.nextLine();
        System.out.println("the path entered is :"+path);
        System.out.println("Enter the name prefix (eg.0000,person...,default press enter use date yyyy-MM-dd):");
        
        String prefix = in.nextLine();
        
        System.out.println("Enter the name separator(eg. - _ ~ + & default press enter use -): ");
        String separator = in.nextLine();
        
        if (separator.isEmpty()) {
            separator = "-";
        }
        
        System.out.println("Enter the suffix(eg. jpg,txt,png...) : ");
        String suffix = in.nextLine();
        File folder = new File(path);
        
        if ( !folder.exists() ) {
            System.out.println("the folder does't exist");
            System.exit(1);
        }else if ( !folder.isDirectory()) {
            System.out.println("Is not a directory!");
            System.exit(2);
        }
        
        Date today = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+separator+
                                            "MM"+separator+"dd");
        String dstName;
        if (prefix.isEmpty()) {
             dstName = sdf.format(today)+separator;
        }else {
             dstName = prefix+separator;
        }
        
        //其实可以使用数组,数组快一点
        List<File> files = Arrays.asList(folder.listFiles());
        
        for(int i = 0 ; i<files.size();i++) {
            files.get(i).renameTo( new File(folder, dstName+(i+1)+suffix) );
        }
        
        
        
    }

}


相关文章

网友评论

      本文标题:批量修改文件名小工具

      本文链接:https://www.haomeiwen.com/subject/mogsmctx.html