如果文件存在且用户输入扩展名,则在 JFileChooser 中重命名文件
Rename file in JFileChooser if file exists and user inputs extension
在用户输入扩展名为 (.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.");
}
嗯,我认为这个条目也可能有用:
Remove filename extension in Java
我目前没有时间来正确测试它(或者更好地测试它)但它不应该这样工作吗:
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;
}
}
我简要地尝试调整了上面提到的帖子中的代码,它很可能包含一些错误或缺陷。 (如果你找到了一些,请不要犹豫,对它们发表评论。其中一些可能是由于我目前没有时间,但我总是愿意学习和改进。)但是我希望这可以帮助你找到合适的解决方案针对您的问题。
好的,所以我已尝试在不过多更改您的代码的情况下完成这项工作。试试这个:
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;
但是,如果用户输入的文件名包含 .
而不是文件扩展名之前的任何地方,这也会出现一些问题。
在用户输入扩展名为 (.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.");
}
嗯,我认为这个条目也可能有用: Remove filename extension in Java
我目前没有时间来正确测试它(或者更好地测试它)但它不应该这样工作吗:
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;
}
}
我简要地尝试调整了上面提到的帖子中的代码,它很可能包含一些错误或缺陷。 (如果你找到了一些,请不要犹豫,对它们发表评论。其中一些可能是由于我目前没有时间,但我总是愿意学习和改进。)但是我希望这可以帮助你找到合适的解决方案针对您的问题。
好的,所以我已尝试在不过多更改您的代码的情况下完成这项工作。试试这个:
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;
但是,如果用户输入的文件名包含 .
而不是文件扩展名之前的任何地方,这也会出现一些问题。