0

我试图在C:\当前硬编码的目录中找到一个字符串。在这个C:\目录中,我希望在根/和子目录中递归地找到输入的字符串(inputString),最后输出包含这些字符串的文件的名称。

我希望以上内容能澄清我最初的问题

我目前有评估.java

import javax.swing.JFrame;

/* Notes: Event - user clicking button, moving mouse
 *  EventHandler - code that response to users action
 * 
 */

class assess {
    public static void main(String args[]){

        assesshelper assessment1 = new assesshelper();
        assessment1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        assessment1.setSize(500,500); // set size of window
        assessment1.setVisible(true);
    }
}

assesshelper.java

import java.awt.FlowLayout; // orientation of screen
import javax.swing.JFrame; // JFrame all basic windows features (min/maximize), title
import javax.swing.JTextField; // Typing text
import javax.swing.JLabel; // output text / images


public class assesshelper extends JFrame {

    private JLabel item1;

    private JTextField theUrl;

    public assesshelper(){ // anything within assesshelper is within window
        super("Assess"); // title of window
        setLayout(new FlowLayout()); // gives us default label

        //item1
        item1 = new JLabel("Welcome!");
        add(item1); // add item1 to the screen


        String setDir = "C:/";
        JTextField inputString = new JTextField();

        inputString.setText("ENTER DIR TO PROCESS");
        /// BEGIN SEARCHING FOR INPUTTED STRING HERE WITHIN ROOT/SUBDIRECTORIES, OUTPUT FILE NAMES CONTAINING STRING

        add(inputString);

    }

}

如何递归搜索inputStringsetDir 设置的目录中的值?

(这里是 Java 新手——请保持建设性)

4

2 回答 2

2

考虑到您是新手,这是一个简单的递归解决方案(但绝不是最有效的):

public static final FileFilter DIRS = new FileFilter() {

  public boolean accept(File file) {
    return file.isDirectory();
  }

};

public static final FileFilter FILES = new FileFilter() {

  public boolean accept(File file) {
    return file.isFile();
  }

}

public void walkTree(File start) throws IOException {

  File[] dirs = start.listFiles(DIRS);
  File[] files = start.listFiles(FILES);

  for (File file : files) {
    if (file.canRead()) {
      if (readAndCheck(file, "string")) {
        // Do Whatever
      }
    }
  }

  for (File dir : dirs) {
    walkTree(dir);
  }

}

public boolean readAndCheck(File file, String string) throws IOException {

  BufferedReader br = null;
  StringBuilder total = new StringBuilder();

  try {

    // If line separators matter in your pattern, consider reading every character
    br = new BufferedReader(new FileReader(file));
    String line;
    while ((line = br.readLine()) != null) {
      total.append(line);
    }

  } finally {
    if (br != null) {
      br.close();
    }
  }

  return total.toString().contains(string);

}

注意:当我在答案框中写下此内容时,可能存在一些语法错误。


此外,您将不得不在新线程上启动它,最好使用SwingWorker

于 2012-10-14T23:52:30.687 回答
0

考虑到 LanguagesNamedAfterCofee 的答案,你可以相对轻松地阅读文件......

public void searchFile(File file, String value) throws IOException {

    FileReader fr = null;
    BufferedReader br = null;
    try {

        fr = new FileReader(file);
        br = new BufferedReader(fr);

        int line = 1;
        String text = null;
        while ((text = br.readLine()) != null) {
            if (text.toLowerCase().contains(value.toLowerCase())) {
                System.out.println("found " + text + " in " + file + " @ [" + line + ":" + text.toLowerCase().indexOf(value.toLowerCase()) + "]");
            }
            line++;
        }
    } finally {
        try {
            fr.close();
        } catch (Exception e) {
        }
        try {
            br.close();
        } catch (Exception e) {
        }
    }
}

现在这个例子将只读取文本文件(它会读取二进制文件,但用处可协商)并打印出找到值的文件和行。

理想情况下,您可以通过某种回调方法、模型或收集信息以供以后使用,将这些信息传回给客户端......

于 2012-10-15T02:18:53.330 回答