简体   繁体   English

使用JFileChooser检查文件是否在特定目录中

[英]Check whether a file is in particular directory or not with JFileChooser

I have make a program where I have to save file in .db format using JFileChooser . 我有一个程序必须使用JFileChooser将文件保存为.db format But the problem is when I create a file with the same name it shows the same message for creating same file or creating a new file. 但是问题是,当我使用相同的名称创建文件时,它会显示相同的消息来表示创建相同文件或创建新文件。 It show the same result False in every case. 在每种情况下,它都显示相同的结果False This is my code for more Clear understanding ! 这是我的代码,可以更清楚地理解!

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);
        }
    }

The Solution is I have to check all files in that directory and match the filename with its extention if any file have the same name which is already in that directory it show message that the file is already exists otherwise it create a new file with that name if file have different name which is not in that directory. 解决方案是,我必须检查该目录中的所有文件,并在文件名和扩展名之间进行匹配,如果该目录中已有文件具有相同的名称,则会显示该文件已存在的消息,否则将使用该名称创建一个新文件如果文件具有不在该目录中的其他名称。

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);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM