我正在尝试在当前硬编码的目录C:\
中找到一个字符串。在这个C:\
目录中,我希望以递归方式在根目录和子目录中找到输入的字符串(inputString),最后输出包含这些字符串的文件的名称。
我希望以上澄清我的初步问题
我目前有 assess.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);
}
}
如何在setDir设置的目录中递归搜索inputString
的值?
(Java-newbie here-请保持建设性)
答案 0 :(得分: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);
}
注意:我在answerbox中写了这个语法错误。
此外,您必须在新线程上启动此功能,最好使用SwingWorker:
答案 1 :(得分: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) {
}
}
}
现在这个例子只会读取文本文件(它会读取二进制文件,但有用性可以协商)并打印出找到值的文件和行。
理想情况下,您可以通过某种回调方法,模型或收集信息将此信息传回客户端以供日后使用...