0

I am creating program whose function is that reading "n" .txt or .java files and of these files creates a UML diagram. I have reading method, but I came to problem with load more same files. I would like to deny load same files, because it makes problems with creating a UML diagram.

I trying to solve it so that I store uploaded file into ArrayList and checking each load file with files saved in ArrayList, where are previous loaded files.

Next problem is that I click to button Yes or No when I choose same file, file is equally loaded.

And when I creating this answer I found next problem. When user select more then one file, ArrayList don't know how to add two files at once.

Is there anyone option how I would solve this problem more easily?

ArrayList<String> filenames = new ArrayList<String>();
JTabbedPane tabbedPaneUML_Files = new JTabbedPane();

private void readFiles() {
    JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);
    FileNameExtensionFilter fileFilter =
            new FileNameExtensionFilter("Only .txt a .java files",
            "txt", "java");
    fc.setFileFilter(fileFilter);
    int returnValue = fc.showOpenDialog(this);        

    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File[] files = fc.getSelectedFiles();
        File file;            
        tabbedPaneUML_Files.addTab("UML diagram", panelUML);

        for (int i = 0; i < files.length; i++) {
            file = files[i];

            for (int j = 0; j < filenames.size(); j++) {
                if (filenames.get(i).equals(fc.getSelectedFile().getName())) {
                    Object[] options = {"Yes", "No"};

                    int answer = JOptionPane.showOptionDialog(this,
                            "Unable to load the same files! To retrieve the other files?",
                            "Load new file", JOptionPane.YES_NO_OPTION,
                            JOptionPane.WARNING_MESSAGE, null, options, options[0]);     

                    if(answer == 0) {
                        readFiles();
                    }
                }
            }         
            filenames.add(file.getName());
            JTextArea loadCode = new JTextArea();
            JScrollPane scrollingFile = new JScrollPane();
            scrollingFile.setViewportView(loadCode);
            tabbedPaneUML_Files.addTab("" + file.getName(), scrollingFile);
            int ch;

            try {
                Reader charsReader =
                        new InputStreamReader(new FileInputStream(file),
                        "UTF-8");

                while ((ch = charsReader.read()) != -1) {
                    loadCode.append(Character.toString((char) ch));
                }
                loadCode.setSelectionStart(0);
                loadCode.setSelectionEnd(0);
                loadCode.setEditable(false);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(HlavniOkno.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(HlavniOkno.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

Thanks for any advice. I already lost ideas. Sorry for my English.

4

1 回答 1

1

您可以使用此处找到的验证文件选择器

并对其进行调整,而不是拥有无效文件名列表,而是拥有已选择文件的列表。然后你可以编辑这部分:

if (file.exists() && getDialogType() == SAVE_DIALOG) {
int confirm = JOptionPane.showConfirmDialog( this, file.getName() + " already exists! Would you like to overwrite it?", "File already exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE );
if (confirm != JOptionPane.YES_OPTION)
return;
}

getDialogType() == LOAD_DIALOG和错误信息:“文件已被加载”例如。

至于多个文件名,请File[] files = chooser.getSelectedFiles();获取所选文件的列表,遍历它们以获取它们的名称,然后将它们存储在已选择文件名的数组中。

编辑- 抱歉刚刚看到你已经完成了File[] files = chooser.getSelectedFiles();,所以你需要做的就是将它添加到包含已加载文件名的数组中。

于 2012-04-07T13:57:12.293 回答