【问题标题】:While searching directory, only first instance of specified file name is shown搜索目录时,仅显示指定文件名的第一个实例
【发布时间】:2014-03-23 18:52:08
【问题描述】:

我正在尝试在用户选择的目录中搜索所有文件。有一个选项可以搜索特定的字符串,并且只有带有这些字符实例的文件名出现在屏幕上。然而,当我运行这个程序时,屏幕上没有出现任何文本(除了使用 JTextArea 构造函数指定的默认值)。如果可能的话,如果有人帮助我调试,我将不胜感激。

package comp.search.direct;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

@SuppressWarnings("serial")
public class SearchInitializer extends JFrame implements ActionListener {

    public static JButton button;
    public static JTextArea ta;
    public static JTextField tf;
    public static JPanel pane, pane2, pane3;
    public static JLabel label;
    public static JCheckBox jcb;
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static ArrayList<String> filenames = new ArrayList();

    @SuppressWarnings("unused")
    public static void main(String[] args) {
    SearchInitializer sci = new SearchInitializer();
    }

    public SearchInitializer() {
    super("Search Directory");
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    }
    this.setLayout(new BorderLayout());

    pane = new JPanel();
    pane.setLayout(new BorderLayout());
    button = new JButton("Search Directory");

    ta = new JTextArea("File Names:");
    ta.setEditable(false);

    tf = new JTextField(10);

    label = new JLabel("Find:");

    jcb = new JCheckBox("Append Text");

    pane2 = new JPanel();
    pane2.add(label);
    pane2.add(tf);

    pane3 = new JPanel();
    pane3.add(button);
    pane3.add(jcb);

    pane.add(pane3, BorderLayout.CENTER);
    pane.add(pane2, BorderLayout.EAST);

    addActionListeners();

    getContentPane().add(pane, BorderLayout.NORTH);
    getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER);

    this.setResizable(true);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(500, 500);
    }

    public static void searchDirectory(File folder, JTextField find, JTextArea ta, ArrayList<String> arg) {
    arg.clear();
    for (File fileentry : folder.listFiles()) {
        if (find.getText() == null) {
        if (fileentry.isDirectory()) {
            searchDirectory(fileentry, find, ta, arg);
        } else {
            if (!fileentry.getName().endsWith(".lnk"))
            ta.append("\n" + fileentry.getName());
        }
        } else {
        if (fileentry.isDirectory()) {
            searchDirectory(fileentry, find, ta, arg);
        } else {
            if (!fileentry.getName().contains(find.getText()))
            return;
            else {
            if (arg.contains(fileentry.getName())) {
                ta.append("\n" + fileentry.getName());
                arg.add(fileentry.getName());
                searchDirectory(folder, find, ta, arg);
                System.out.println(fileentry.getName()); // Testing purposes only
            } else {
                return;
            }
            }
        }
        }
    }
    }

    public void addActionListeners() {
    button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button) {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (fc.showDialog(SearchInitializer.this, "Select") ==     JFileChooser.APPROVE_OPTION) {
        searchDirectory(new File(fc.getSelectedFile().getAbsolutePath()), tf, ta, filenames);
        }
    }

    }
}

【问题讨论】:

  • 不要在你的问题中加入不相关的代码。
  • 如果我认为这段代码无关紧要,我就不会包含它。我试图通过包含尽可能多的代码来提供帮助。我很抱歉。

标签: java file search directory


【解决方案1】:

错误大概在这里:

        if (arg.contains(fileentry.getName())) {
            ta.append("\n" + fileentry.getName());
            arg.add(fileentry.getName());
            searchDirectory(folder, find, ta, arg);
            System.out.println(fileentry.getName()); // Testing purposes only
        } else {
            return;
        }

您告诉它添加一个条目到ArrayList,前提是它已经存在。由于这种逻辑,您的ArrayList 永远不会是空的。

【讨论】:

  • 我通过将 if (arg.contains(fileentry.getName())) { 更改为 if (!arg.contains(fileentry.getName())) { 好像它仍然没有收到堆栈溢出添加文件名
  • @fmi11 你有一个不属于的递归调用。
  • 它是递归的,因为我需要它来搜索指定字符的下一个实例。无需再次调用该方法,它只会给我第一个实例。
  • @fmi11 当您使用相同的folder 参数递归调用它时,递归调用以folder.listFiles() 开始,并且将从头开始,而不是调用者中的迭代器停止的地方. for 循环不会找到剩余的实例吗?另外,请确保您没有用您的return 语句过早地切断for 循环。我没有彻底研究过这个,但我认为你的逻辑需要重新思考。除了进入子文件夹之外,您不应该使用递归。
  • 好的,我会调查的。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-08-17
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2012-01-21
  • 1970-01-01
相关资源
最近更新 更多