2012-04-19 168 views
0

我在JFileChooser的JList中添加了一些文件。我添加了一个名为“CHECK”的新按钮,单击它时会告诉JList中是否存在特定文件(已添加的文件中)。如果你们中的任何一位能够告诉我什么是正确的做法,这将是非常好的。检查JList中是否存在文件

感谢您提前。

这是我目前的代码;

  final JFileChooser fileChooser = new JFileChooser(); 
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); 
    fileChooser.setMultiSelectionEnabled(true); 
    getContentPane().add(fileChooser, "cell 0 0 3 9"); 

    JScrollPane scrollPane = new JScrollPane(); 
    getContentPane().add(scrollPane, "cell 10 1 3 8,grow"); 

    vector = new Vector<File>(); 
    final JList list = new JList(vector); 
    scrollPane.setViewportView(list); 

    JPanel panel = new JPanel(); 
    getContentPane().add(panel, "cell 3 4 7 1,grow"); 

    JButton btnNewButton = new JButton("Add Files"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      for (File file : fileChooser.getSelectedFiles()) { 
         vector.add(file); 
         System.out.println("Added..!!"); 
       } 
       list.updateUI(); 

      } 
    }); 
    panel.add(btnNewButton); 

    JButton btnNewButton_1 = new JButton("Remove Files"); 
    btnNewButton_1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if(list.getSelectedIndices().length > 0) { 
        int[] selectedIndices = list.getSelectedIndices(); 
        for (int i = selectedIndices.length-1; i >=0; i--) { 
         vector.removeElementAt(i); 
         System.out.println("Removed..!!"); 
        } 
        } 
        list.updateUI(); 

     } 
     }); 
    panel.add(btnNewButton_1); 

    JButton btnNewButton_2 = new JButton("Check For Files"); 
    btnNewButton_2.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      String name = ""; 
      if(list.getSelectedIndices().length > 0) { 
         //// to check if a file exists ///// 
      } 
      } 
      }); 

    panel.add(btnNewButton_2); 
+0

'如果(file.isExists())'不工作? – 2012-04-19 11:18:57

+0

但是这只是检查一个文件是否存在,一般来说..我想知道如果文件存在或不在JList中已经有一些文件ih,因为我在第一步添加了它们。 – dmurali 2012-04-19 11:20:35

+0

我们很难在没有显示代码的情况下提供帮助。你能发表你迄今为止所尝试过的/已有的吗? – Jim 2012-04-19 11:29:44

回答

1

矢量有一个包含方法,您可以使用:

if(vector.contains(file)){ 
    //Vector has the file 
} 
+0

我认为这可能只有当文件不包含完整路径,只有文件名或包含字符串值而不是文件 – mKorbel 2012-04-19 11:41:47

+0

是的。你说得对。我能够检查存在..但在那种情况下,我需要将整个路径指定为输入,以及如果我不知道它存在的路径,该怎么办?例如,我指定像; File file = new File(“H:\\ abc.txt”); \t if(vector.contains(file)){---} – dmurali 2012-04-19 11:49:20

1
  1. 请使用集合框架的List为VAR类型和ArrayList的具体类实例化。从Java 1.2开始,Vector一直是过去的事情。
  2. 迭代选择的文件在两端检查规范的路径:

final File toCheck = fileToCheckInList.getCanonicalFile(); 
for (File file : fileChooser.getSelectedFiles()) 
    if (file.getCanonicalFile().equals(toCheck)) return true; 
+0

感谢您的建议。但即使在这种情况下,我也要指定路径,然后才能正确检查存在。如果我以前不知道路径(文件的路径)怎么办? – dmurali 2012-04-19 12:02:24

+0

我们现在进入您没有提供详细信息的细节。究竟你用什么字符串搜索,以及列表中的字符串究竟是什么? (当我说字符串时,我用它松散地,我知道你有文件在那里)。 – 2012-04-19 12:03:50

+0

我在我的JList中有几个文件,比如zip文件和xml文件。我总共添加了3个文件,2个xml文件是一个十六进制文件,其中我想要搜索一个特定的xml文件是否存在。 – dmurali 2012-04-19 12:09:26