如何使用java读取目录中文件的内容,并返回具有特定关键字的文件



在我的代码中,我可以列出电脑上文件夹中的所有文件,但要检查关键字是否存在于我在StringBuffer中使用indexOf((的文件中。我面临的问题是,没有打印出具有该关键字的文件名的期望输出。我找不到错误在哪里,也找不到我犯了什么错误。

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class ListOfFiles {

public static void main(String args[]) throws IOException {
// Creating a File object for directory
File directoryPath = new File("C:\Users\nanis\Downloads\New folder");
// List of all files and directories
File filesList[] = directoryPath.listFiles();
// System.out.println("List of files and directories in the specified directory:");
Scanner sc = null;

for (File file: filesList) {
// System.out.println("File name: "+file.getName());
// System.out.println("File path: "+file.getAbsolutePath());
// System.out.println("Size :"+file.getTotalSpace());
// Instantiating the Scanner class
sc = new Scanner(file);
String input;
StringBuffer sb = new StringBuffer();

while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input + " ");
int integer = sb.indexOf("VM"); // the keyword is "VM" that I want to search
if (integer > 0) {
System.out.println("keyword is present in " + file.getAbsolutePath())
}
}
}
}
}

我认为问题出在你的扫描仪上。您正在获取用户输入input = sc.nextLine();,为此需要System.in。此外,如果我尝试使用sc打印某些内容,它将不打印任何内容,这将使while (sc.hasNextLine())为假,因此您的算法存在问题。

你在做搜索算法,对吧?但是在for each循环中插入while循环。这不起作用,因为在for each循环的第一次迭代中,只有第一个文件存在,因此如果搜索关键字是针对最后一个文件的,则为false。

一个基本的搜索算法是:

1. Input keyword to find
2. Loop through the lists to check if found
3. return either true or false

编辑:您要搜索.txt文件。这是同一个概念。你需要先把它们全部储存起来。存储时不搜索。如果你已经存储了它们,你现在可以搜索它们了。

我更改了代码以读取txt文件。此外,您的目录应该只包含.txt文件,否则它将无法工作,因为您正在读取文件的内容。在您的情况下,HashMap会很有用,因为您需要存储两个值。文件名及其内容。

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner;
public class ListOfFiles {
public static void main(String args[]) throws IOException {
// Creating a File object for directory
File directoryPath = new File("C:\Users\nanis\Downloads\New folder\");
// List of all files and directories
File filesList[] = directoryPath.listFiles();
// System.out.println("List of files and directories in the specified
// directory:");
Scanner sc = new Scanner(System.in);
Scanner myReader = null;
HashMap<String, String> fff = new HashMap<String, String>(); // storage for the file name and content
for (File file : filesList) {
File myObj = new File("C:\Users\nanis\Downloads\New folder\" + file.getName());
myReader = new Scanner(myObj);
String read = "";
while (myReader.hasNextLine()) {
read += myReader.nextLine();
}
fff.put(file.getName(), read); // store file name and its contents
}
System.out.print("Search The File By Keyword:");
String find = sc.nextLine();
for (String i : fff.keySet()) {
if (fff.get(i).contains(find)) { // check the contents if it contains the keyword u are search for
File found = new File("C:\Users\nanis\Downloads\New folder\" + i);
System.out.println("keyword is present in " + found.getAbsolutePath());
}
}
sc.close();
myReader.close();
}
}

最新更新