gpt4 book ai didi

java - JFileChooser:防止用户以现有名称保存文件

转载 作者:行者123 更新时间:2023-11-30 04:18:58 27 4
gpt4 key购买 nike

我正在使用 JFileChooser 保存 textArea 中的数据,并且我想阻止用户以现有名称保存新文件。每次我执行代码时,它只会提示用户一次更改他们尝试保存的文件的名称。我可以通过什么方式使用循环来阻止用户使用现有文件名,直到他们输入新名称?这是我到目前为止所拥有的:

JButton OKSavebutton = new JButton("OK");
OKSavebutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

final JFileChooser fc = new JFileChooser();
final int result = JOptionPane.showConfirmDialog(fc, "Save new Observation Well File?", "Save File",
JOptionPane.OK_CANCEL_OPTION);
fc.setCurrentDirectory(new File("C:/Users/281925/Desktop/mads/User Saved Internal Contamination Problem/Observation Wells"));

FileNameExtensionFilter madsType = new FileNameExtensionFilter("MADS file (*.mads)", "mads");
fc.setFileFilter(madsType);
fc.addChoosableFileFilter(madsType);
fc.setAcceptAllFileFilterUsed(false);

int returnVal = fc.showSaveDialog(fc);
File f = new File(fc.getSelectedFile()+".mads");

switch(result){
case JOptionPane.OK_OPTION:
if (f.exists()){
int result1 = JOptionPane.showConfirmDialog(fc, "The file name exists. Please input new File name", "New File Name",
JOptionPane.OK_CANCEL_OPTION);
fc.showSaveDialog(fc);
}
try{
String fileExt = ".mads";
//create a buffered writer to write to a file
BufferedWriter out = new BufferedWriter(new FileWriter(fc.getSelectedFile().getPath() + fileExt));
out.write(textArea.getText());//write contents of the TextArea to the file
out.close();//close the file stream
}
catch(Exception e){ //catch any exceptions and write to the console
System.out.println(e.getMessage());
}

return;
case JOptionPane.CANCEL_OPTION:
fc.cancelSelection();
return;
default:
return;
}

}

});

我已经解决这个问题两天了,我真的需要帮助!请并谢谢您!

这是编辑后的代码。感谢@luk2302 的帮助。我确实需要稍微调整一下,但现在它就像一个魅力:)

int result1 = fc.showSaveDialog(fc);
File f = new File(fc.getSelectedFile()+".mads");

/* loop until the user entered a file that does not exist yet */
while(f.exists()) {

result = JOptionPane.showConfirmDialog(fc, "The file name exists. Please input new File name", "New File Name", JOptionPane.OK_CANCEL_OPTION);

if(result == JOptionPane.OK_OPTION){
fc.showSaveDialog(fc);
}
/*Create new file and set it equal to f*/
File f1 = new File(fc.getSelectedFile() + ".mads");
f = f1;
}

/* return if user cancels */
if(result == JOptionPane.CANCEL_OPTION) {
fc.cancelSelection();
return;
}
/* if the user finally selected a non existing file do whatever needs to be done. */
if (result == JOptionPane.OK_OPTION) {
try {
String fileExt = ".mads";
//create a buffered writer to write to a file
BufferedWriter out = new BufferedWriter(new FileWriter(fc.getSelectedFile().getPath() + fileExt));
out.write(textArea.getText());//write contents of the TextArea to the file
out.close();//close the file stream
} catch(Exception e){ //catch any exceptions and write to the console
System.out.println(e.getMessage());
}
return;
}

最佳答案

只需执行以下操作:

/* loop until the user entered a file that does not exist yet */
while(fc.getSelectedFile().exists()) {
result = JOptionPane.showConfirmDialog(fc, "The file name exists. Please input new File name", "New File Name", JOptionPane.OK_CANCEL_OPTION);
fc.showSaveDialog(fc);
/* return if user cancels */
if(result == JOptionPane.CANCEL_OPTION) {
fc.cancelSelection();
return;
}
}

/* if the user finally selected a non existing file do whatever needs to be done. */
if (result == JOptionPane.OK_OPTION) {
try {
String fileExt = ".mads";
//create a buffered writer to write to a file
BufferedWriter out = new BufferedWriter(new FileWriter(fc.getSelectedFile().getPath() + fileExt));
out.write(textArea.getText());//write contents of the TextArea to the file
out.close();//close the file stream
} catch(Exception e){ //catch any exceptions and write to the console
System.out.println(e.getMessage());
}
return;
}

另请注意,您分配的 int returnVal = fc.showSaveDialog(fc); 从未使用过,而是在代码的其余部分中使用 result 的值。因此,将变量重命名为 `int result = fc.showSaveDialog(fc);`

关于java - JFileChooser:防止用户以现有名称保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17604360/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com