我制作了一个程序,我必须使用.db format
在JFileChooser
中保存文件。但问题是,当我创建一个具有相同名称的文件时,它会显示相同的消息,用于创建相同的文件或创建新文件。它在每种情况下都显示相同的结果False
。这是我更清晰理解的代码!
public void actionPerformed(ActionEvent arg0) {
int answer=JOptionPane.showConfirmDialog(null, "Do You Want To Create New Company ?","Confirm Dialog",JOptionPane.YES_NO_OPTION);
switch(answer)
{
case JOptionPane.YES_OPTION:
File file=new File("Database");
file.mkdirs();
JFileChooser fc = new JFileChooser(file);
fc.setDialogTitle("Enter Inventory Plus File Name");
fc.setMultiSelectionEnabled(false);
fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
FileTypeFilter filefilter=new FileTypeFilter(".db","database");
fc.setFileFilter(filefilter);
int result = fc.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File fileName=fc.getSelectedFile();
String filename=fileName.getName();
String filepath=fileName.getParent();
if(fileName.exists())
{
String message="File "+filename+".db"+" already exist in\n"+filepath+"\nDo you want to Overwrite?\n";
int reply=JOptionPane.showConfirmDialog(null, message,"Warning",JOptionPane.YES_NO_OPTION);
if(reply==JOptionPane.YES_OPTION)
{
try {
fileName.delete();
BufferedWriter writer=new BufferedWriter(new FileWriter(fileName+".db"));
writer.close();
JOptionPane.showMessageDialog(null, "File "+filename+".db"+" is Overwritten Successfully in\n"+filepath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
{
try {
BufferedWriter writer=new BufferedWriter(new FileWriter(fileName+".db"));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
case JOptionPane.NO_OPTION:
}
}
public class FileTypeFilter extends FileFilter{
private final String Extension;
private final String Description;
@Override
public boolean accept(File file) {
if(file.isDirectory()) {
return true;
}
return file.getName().endsWith(Extension);
}
public FileTypeFilter(String extension, String description) {
super();
Extension = extension;
Description = description;
}
@Override
public String getDescription() {
return Description + String.format("(*%s)", Extension);
}
}
答案 0 :(得分:0)
解决方法是我必须检查该目录中的所有文件,并将文件名与其扩展名匹配,如果任何文件具有相同的名称,该文件已在该目录中,则显示该文件已存在的消息,否则创建新文件如果文件具有不在该目录中的不同名称,则使用该名称。
public void actionPerformed(ActionEvent arg0) {
int answer=JOptionPane.showConfirmDialog(null, "Do You Want To Create New Company ?","Confirm Dialog",JOptionPane.YES_NO_OPTION);
switch(answer)
{
case JOptionPane.YES_OPTION:
File file=new File("Database");
file.mkdirs();
JFileChooser fc = new JFileChooser(file);
fc.setDialogTitle("Enter Inventory Plus File Name");
fc.setMultiSelectionEnabled(false);
fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
FileTypeFilter filefilter=new FileTypeFilter(".db","database");
fc.setFileFilter(filefilter);
int result = fc.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File[] dircontent=file.listFiles();
File fileName=fc.getSelectedFile();//Database\Second
String filename=fileName.getName();//second,first
String filepath=fileName.getParent();//database
for(int i=0;i<dircontent.length;i++)
{
if(dircontent[i].getName().equals(filename+".db"))
{
String message="File "+filename+".db"+" already exist in\n"+filepath+"\nDo you want to Overwrite?\n";
int reply=JOptionPane.showConfirmDialog(null, message,"Warning",JOptionPane.YES_NO_OPTION);
if(reply==JOptionPane.YES_OPTION)
{
try {
fileName.delete();
BufferedWriter writer=new BufferedWriter(new FileWriter(fileName+".db"));
writer.close();
JOptionPane.showMessageDialog(null, "File "+filename+".db"+" is Overwritten Successfully in\n"+filepath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
{
try {
BufferedWriter writer=new BufferedWriter(new FileWriter(fileName+".db"));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
case JOptionPane.NO_OPTION:
}
}
public class FileTypeFilter extends FileFilter{
private final String Extension;
private final String Description;
@Override
public boolean accept(File file) {
if(file.isDirectory()) {
return true;
}
return file.getName().endsWith(Extension);
}
public FileTypeFilter(String extension, String description) {
super();
Extension = extension;
Description = description;
}
@Override
public String getDescription() {
return Description + String.format("(*%s)", Extension);
}
}