java将增量编号添加到文件存在之前

java将增量编号添加到文件存在之前,java,filesystems,Java,Filesystems,我需要添加一个递增的数字到文件存在之前,我知道如何检查这一点,以及如何添加在最后的数字,但我需要一个想法如何定罪的数字每次 看看这个,例如,了解我 存在C:\MyFiles\File.pdf,请尝试C:\MyFiles\File1.pdf 下次呢 C:\MyFiles\File2.pdf 不知道如何得到最后的增量 请看我的代码,我只能增加一次 int indexOfDot = fName.indexOf("."); //if theres a dot, start one cha

我需要添加一个递增的数字到文件存在之前,我知道如何检查这一点,以及如何添加在最后的数字,但我需要一个想法如何定罪的数字每次

看看这个,例如,了解我 存在C:\MyFiles\File.pdf,请尝试C:\MyFiles\File1.pdf 下次呢 C:\MyFiles\File2.pdf

不知道如何得到最后的增量

请看我的代码,我只能增加一次

    int indexOfDot = fName.indexOf(".");
    //if theres a dot, start one character before it, otherwise,
    //start at the end of the string
    int startIndex = indexOfDot > -1 ? indexOfDot - 1 : fName.length() - 1;
    if(indexOfDot > -1)
         fileNameSuffix = fName.substring(indexOfDot, fName.length());
    //search backward on the name to see if it already
    //has a number and count the digits
    StringBuffer digits = new StringBuffer();
    char character = 0;
    for (int i = startIndex; i >= 0; i--)
    {
         character = fName.charAt(i);
         if (Character.isDigit(character))
         {
              digits.append(character);
         }
    }
    if (digits.length() > 0)
    {
          //There where already digits in the filename.
          //Create a number from the digits and add 1
          Integer newNumber =new Integer(Integer.parseInt(digits.toString()) + 1);
          //Then append that to the actual filename
          String actualFileName = fName.substring(0, startIndex + 1 - digits.length());
          rtnNewFileName = actualFileName + newNumber.toString() + fileNameSuffix;
    }else
    {
          //There where no digits in the filename
          //Append 1 to the actual filename
          String actualFileName = fName.substring(0, startIndex - digits.length());
          rtnNewFileName = actualFileName + "1" + fileNameSuffix;
   }

   file.setFileName(rtnNewFileName);

   out.println("<li>File Uploaded <br/>Form field : uploadfile"+"<BR> Uploaded file : "+file.getFileName()+" ("+file.getFileSize()+" bytes)"+"<BR> Content Type : "+file.getContentType());

    //Uses the bean now to store specified by jsp:setProperty at the top.
    upBean.store(mrequest, "uploadfile");
int indexOfDot=fName.indexOf(“.”);
//如果有一个点,在它前面开始一个字符,否则,
//从字符串的末尾开始
int startIndex=indexOfDot>-1?indexOfDot-1:fName.length()-1;
如果(indexOfDot>-1)
fileNameSuffix=fName.substring(indexOfDot,fName.length());
//向后搜索该名称,查看它是否已存在
//有一个数字并计算数字
StringBuffer位=新的StringBuffer();
字符=0;
对于(int i=startIndex;i>=0;i--)
{
character=fName.charAt(i);
if(字符.isDigit(字符))
{
数字。追加(字符);
}
}
if(digits.length()>0)
{
//文件名中已经有个数字。
//从数字中创建一个数字,然后加1
整数newNumber=新整数(Integer.parseInt(digits.toString())+1);
//然后将其附加到实际文件名
String actualFileName=fName.substring(0,startIndex+1-digits.length());
rtnNewFileName=actualFileName+newNumber.toString()+fileNameSuffix;
}否则
{
//文件名中没有数字
//将1附加到实际文件名
String actualFileName=fName.substring(0,startIndex-digits.length());
rtnNewFileName=实际文件名+“1”+文件名uffix;
}
setFileName(rtnNewFileName);
out.println(“
  • 上传文件
    表单字段:上传文件”+”
    上传文件:“+File.getFileName()+”(“+File.getFileSize()+”字节)”+”
    内容类型:“+File.getContentType()); //现在使用bean在顶部存储jsp:setProperty指定的内容。 存储(mrequest,“上传文件”);
  • 您需要第二次将File2.pdf传递给该方法,或者检查生成的文件名是否存在,然后使用新文件名重复该方法

    顺便说一句,我会使用正则表达式:

    Pattern p = Pattern.compile("(.*?)(\\d+)?(\\..*)?");
    do{
        Matcher m = p.matcher(fName);
        if(m.matches()){//group 1 is the prefix, group 2 is the number, group 3 is the suffix
            fName = m.group(1) + (m.group(2)==null?1:(Integer.parseInt(m.group(2)) + 1)) + (m.group(3)==null?"":m.group(3));
        }
    }while(new File(fName).exists());//repeat until a new filename is generated
    

    您好,我找到了答案,您只需在测试后加载的每个新文件之前添加(C0Py0),如果您的文件超出了调用方法changing()的范围


    我希望这能帮助她

    如果你想保留第一个没有数字后缀的文件名,这对我来说很有效(基于rachet的回答)

    for (i = (len - 1); i >= 0; i--)
      dest.append(source.charAt(i));
    return dest.toString();
    
      invName=reverseIt(newName);
    
                                    while(k != invName.length()){
    
                                     if( invName.substring(j,k).equals("yP0C")){
                                                                  copyf=invName.substring(0,j);
                                                                  k=invName.length()-1;
    
                                                                            }
                                     else{
                                         j++;
                                         k++;
                                        }
    
                                                                 }
    increment=reverseIt(copyf);
    //System.out.println(increment);
    incr=Integer.parseInt(increment);
    incr+=1;
    newName=newName.substring(0,newName.length()-(j-1));
    increment=String.valueOf(incr);
    finalName=newName+increment+"."+ext;
    
    Pattern p = Pattern.compile("(.*?)(\\d+)?(\\..*)?");
    
    while(new File(pathname).exists()){
        Matcher m = p.matcher(pathname);
        if(m.matches()){//group 1 is the prefix, group 2 is the number, group 3 is the suffix
            pathname = m.group(1) + (m.group(2)==null?1:(Integer.parseInt(m.group(2)) + 1)) + (m.group(3)==null?"":m.group(3));
        }
    }