2014-02-25 30 views
0

這是我的代碼,用它的文件名搜索文本文件。 這個程序沒有錯誤,但我不知道代碼是否正確搜索。如果在消息對話框中沒有找到「文件未找到」文件,我想獲取「找到文件」消息。所以任何一個對我有困難有想法的人都請幫助我。如何在if語句條件中使用搜索到的文本文件?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    // TODO add your handling code here: 

    File dir = new File("C:\\Users\\EmuD\\Documents\\NetBeansProjects\\Bank Project"); 
    File[] matchingFiles = dir.listFiles(new FilenameFilter() { 

     public boolean accept(File dir, String name) { 
      String search = jTextField1.getText(); 
      return name.startsWith(search) && name.endsWith(".txt"); 
     } 
    }); 
} 

非常感謝!

+0

有什麼困難? –

回答

0

File有一個exists()方法。要從這些參數中獲得File,請創建一個新文件。代碼會喜歡這樣的東西boolean fileExists = new File(dir, fileName).exists();

我想你不是在尋找如何在JLabel中顯示測試的幫助。

0

回答你的問題,可以發現here

File f = new File(filePathString); 
if(f.exists() && !f.isDirectory()) { /* do something */ } 

你不應該只使用File.exists(),因爲它返回的也是如此目錄。由於您只對文件感興趣,因此您需要進行第二次檢查!File.isDirectory()

相關問題