Java 获取带有日期和尾随计数器的文件名

Java 获取带有日期和尾随计数器的文件名,java,filenames,Java,Filenames,我需要将文件名设为name\uu N,其中N是类似文件数量的计数器 String fileName = "name"; String fileNameWithDate = fileName + "_"+ new SimpleDateFormat("MMM_dd_yyyy").format(cal.getTime()); 如果我尝试为给定日期下载更多次,则计数应增加1;例如,name\u Dec\u 10\u 2015\N。如何获取带有日期且递增1的文件名?正如@MadProgrammer已经编

我需要将文件名设为
name\uu N
,其中N是类似文件数量的计数器

String fileName = "name";
String fileNameWithDate = fileName + "_"+ new SimpleDateFormat("MMM_dd_yyyy").format(cal.getTime());

如果我尝试为给定日期下载更多次,则计数应增加1;例如,
name\u Dec\u 10\u 2015\N
。如何获取带有日期且递增1的文件名?

正如@MadProgrammer已经编写的那样,您需要按照以下步骤进行操作

  • 获取与您的模式匹配的所有文件的列表
  • 查找计数器最高的文件
  • 为增加的计数器创建了文件名
下面是一个简短的片段,您可以将其用作起点(边缘情况的处理,如:未找到文件等,故意省略,这是您自己工作的一部分)

公共类递增计数器{
静态最终SimpleDataFormat文件_日期=新SimpleDataFormat(“MMM_dd_yyyy”);
公共静态void main(字符串[]args){
//筛选与给定模式匹配的文件
FilenameFilter FilenameFilter=新FilenameFilter(){
@凌驾
公共布尔接受(文件目录,字符串名称){
返回name.matches(“^name\u…\ u…\ u…*\\.txt”);
}
};
//需要根据文件计数器对文件列表进行排序
比较器sortByCounter=新比较器(){
@凌驾
公共int比较(文件file1、文件file2){
返回getFileCounter(file1.getName())-getFileCounter(file2.getName());
}
};
字符串filesLocation=“resources/”;
int highestCounter=0;
//读取与筛选器匹配的文件
File[]listFiles=新文件(filesLocation).listFiles(fileNameFilter);
List files=Arrays.asList(listFiles);
//按文件计数器对文件进行排序
Collections.sort(文件、sortByCounter);
//得到最高的计数器
String fileWithHighestCounter=files.get(files.size()-1.getName();
highestCounter=getFileCounter(fileWithHighestCounter);
String nextFileName=String.format(“name\u%s\u%d.txt”,
文件\u DATE.format(新日期()),
最高计数器+1
);
System.out.println(“nextFileName=“+nextFileName”);
}
/**
*提取文件计数器。
*@param fileName文件名
*@return 0-如果文件没有计数器,则计数器值
*/
静态int getFileCounter(字符串文件名){
字符串namePatternWithCounter=“^name\u…\ u…\ u*(.*)\\.txt”;
字符串countString=fileName.replaceAll(namePatternWithCounter,“$1”);
if(counterString.isEmpty()){
返回0;
}
返回整数.parseInt(反字符串);
}
}

文件#存在
文件#列表文件(文件过滤器)
将在这里对您有所帮助…我不明白。你能提供一个例子吗?你需要尝试列出所有与你使用的前缀匹配的文件。当有超过一个的时候,你会想知道有多少,然后增加数字…如果有任何代码示例,你的意思是
public class IncreaseCounter {

    static final SimpleDateFormat FILE_DATE = new SimpleDateFormat("MMM_dd_yyyy");

    public static void main(String[] args) {

        // filter files which matches the given pattern
        FilenameFilter fileNameFilter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.matches("^name_..._.._.....*\\.txt");
            }
        };

        // needed to sort the file list based on the file counter
        Comparator<File> sortByCounter = new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                return getFileCounter(file1.getName()) - getFileCounter(file2.getName());
            }
        };

        String filesLocation = "resources/";
        int highestCounter = 0;

        // read the files matching the filter
        File[] listFiles = new File(filesLocation).listFiles(fileNameFilter);
        List<File> files = Arrays.asList(listFiles);

        // sort the files by their file counter
        Collections.sort(files, sortByCounter);

        // get the highest counter
        String fileWithHighestCounter = files.get(files.size() - 1).getName();
        highestCounter = getFileCounter(fileWithHighestCounter);

        String nextFileName = String.format("name_%s_%d.txt",
                FILE_DATE.format(new Date()), 
                highestCounter + 1
        );

        System.out.println("nextFileName = " + nextFileName);
    }

    /**
     * Extract the file counter.
     * @param fileName the file name
     * @return 0 - if the file has no counter, otherwise the counter value
     */
    static int getFileCounter(String fileName) {
        String namePatternWithCounter = "^name_..._.._...._*(.*)\\.txt";
        String counterString = fileName.replaceAll(namePatternWithCounter, "$1");
        if (counterString.isEmpty()) {
            return 0;
        }
        return Integer.parseInt(counterString);
    }
}