Java 如果文件存在且用户输入扩展名,则重命名JFileChooser中的文件

Java 如果文件存在且用户输入扩展名,则重命名JFileChooser中的文件,java,jfilechooser,file-extension,Java,Jfilechooser,File Extension,在用户输入一个扩展名为(.txt)的文件名并且该文件名已经存在之前,该代码将一直工作。因此,如果文件“test.txt”存在,并且用户决定将新文件命名为“test”,则该文件将命名为“test(1.txt)”,但如果用户添加扩展名,如“test.txt”,则该文件将命名为“test.txt”,下一个文件用户名“test.txt”将保存为“test.txt(1.txt)”。 是否可以从 JFileChooser < /代码>中获取文件名,如果用户输入它并将其扩展为新文件名,则删除它的扩展名,在原始

在用户输入一个扩展名为(.txt)的文件名并且该文件名已经存在之前,该代码将一直工作。因此,如果文件“test.txt”存在,并且用户决定将新文件命名为“test”,则该文件将命名为“test(1.txt)”,但如果用户添加扩展名,如“test.txt”,则该文件将命名为“test.txt”,下一个文件用户名“test.txt”将保存为“test.txt(1.txt)”。 是否可以从<代码> JFileChooser < /代码>中获取文件名,如果用户输入它并将其扩展为新文件名,则删除它的扩展名,在原始文件名的中间加上编号并将其扩展?我可以得到字符串类型的名称,而不需要扩展名,但我需要它作为文件类型

         File ft = fc.getSelectedFile();
                String ext = ".txt";
                File tmp = new File(ft.getPath());
                if (!fc.getSelectedFile().getAbsolutePath().endsWith(ext)){ 
                    ft = new File (ft + ext);
                }       
                File test =  new File(ft.getPath());
                File temp = new File(ft.getPath());
                File temp1 = new File(ft.getPath());
                int count = 1;
                while (temp.exists()) {
                    if(tmp.getAbsolutePath().endsWith(ext)){

                    }
                    File ft1 = new File (tmp + "(" + count + ")");
                    ft = new File (tmp + "(" + count + ")" + ext);
                    count++;
                    temp = new File(ft.getPath());
                    temp1 = new File(ft1.getPath());
                }
                if (!temp1.getAbsolutePath().endsWith(ext)){ 
                    ft = new File (temp1 + ext);
                }
                int cnt = count - 1;
                if (!test.equals(temp)){
                JOptionPane.showMessageDialog(null, "File already exists. So it's saved with (" + cnt + ") at the end.");               
                }   

嗯,我认为这个条目也很有用:

我目前没有时间对它进行适当的测试(或者更好的测试),但它不应该这样工作吗:

public static String removeExtention(File f) {

    String name = f.getName();

    // Now we know it's a file - don't need to do any special hidden
    // checking or contains() checking because of:
    final int lastPeriodPos = name.lastIndexOf('.');
    if (lastPeriodPos <= 0)
    {
        // No period after first character - return name as it was passed in
        return f;
    }
    else
    {
        // Remove the last period and everything after it
        File renamed = new File(f.getParent(), name.substring(0, lastPeriodPos));
        return renamed;
    }
}
publicstaticstringremoveextention(文件f){
String name=f.getName();
//现在我们知道它是一个文件-不需要做任何特殊的隐藏
//检查或包含()检查,因为:
final int lastPeriodPos=name.lastIndexOf('.');

如果(lastPeriodPos正常,那么我已尝试在不太修改代码的情况下实现此功能。请尝试以下操作:

String filePath = fc.getSelectedFile().getAbsolutePath();
final String ext = ".txt";
String filePathWithoutExt;

if (filePath.endsWith(ext)) {
    filePathWithoutExt = filePath.substring(0, filePath.length() - ext.length());
} else {
    filePathWithoutExt = filePath;
}

File test = new File(filePathWithoutExt + ext);
File temp = new File(filePathWithoutExt + ext);

int count = 0;

while (temp.exists()) {
    count++;
    temp = new File(filePathWithoutExt + "(" + count + ")" + ext);
}

if (!test.equals(temp)) {
    JOptionPane.showMessageDialog(null,
        "File already exists. So it's saved with (" + count + ") at the end.");
}
编辑:

根据Marco N的建议。最好通过查找
的最后一个位置来确定是否存在扩展名,因为这也适用于“.txt”以外的扩展名。然后使用此值拆分字符串。替换代码如下所示:

final int lastPeriodPos = filePath.lastIndexOf(".");

if (lastPeriodPos >= 0) {
    filePathWithoutExt = filePath.substring(0, lastPeriodPos);
} else {
    filePathWithoutExt = filePath;

但是,如果用户输入的文件名包含
文件扩展名之前以外的任何位置,这也会有一些问题。

您可以在此处找到一些帮助(阅读不同的答案):他们都使用字符串类型变量,但这对我不起作用,或者至少我不知道如何在我的案例中使用它们。我被告知这一点,但我如何使用它来更改变量ft,即文件类型?我不确定我是否100%正确地获得了您的用例。但据我所知,您有一个从获取的
文件
变量e> JFileChooser
。您的问题是,如果存在重复的扩展名,您需要添加一个(1)或任何其他扩展名。据我所知,这正是上述方法所做的。您插入一个
文件
变量。第一个
if
调用应该检测是否存在扩展名。如果不存在,请插入代码以添加(1)并返回新的
文件
。如果存在类似txt的扩展名,请使用
else
大小写追加(1)或(2)并返回重命名的文件。否则,请澄清。