简体   繁体   English

如何在Java中制作一个小型搜索引擎

[英]how to make a small search engine in java

I want to make a small search engine.consider that I have 5 text files and I want to find the word "book" in whole files.How should i start? 我想做一个小型搜索引擎。考虑到我有5个文本文件,并且我想在整个文件中找到“ book”一词。我应该如何开始? I am a senior and I have a little information about trees and iterators and I have to use these concepts.could anyone help me with the code? 我是一名大四学生,我对树和迭代器有一些了解,我必须使用这些概念。有人可以帮助我编写代码吗?

You do not need any knowledge about trees and iterators. 您不需要有关树和迭代器的任何知识。 It is all about recursive calls, no more. 全部与递归调用有关,仅此而已。

Here is the pure Java code with no dependencies or frameworks used: just pass directory or file name and word you'd like to search as parameters when call it. 这是不使用任何依赖项或框架的纯Java代码:只需在调用它时传递目录或文件名以及您想搜索的单词作为参数即可。

package com.mytests;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Sometest {

public static void main(String[] args) {

    if (null == args || args.length < 2) {
        System.err.println("Not enough parameters. Please provide searchPath and searchWord.");
        return;
    }

    String pathToSearch = args[0];
    String wordToSearch = args[1];

    System.out.println("Start search word " + wordToSearch + " in " + pathToSearch);

    File searchFile = new File(pathToSearch);

    if (!searchFile.exists()) {
        System.err.println("Cannot find given file or directory to search :" + pathToSearch + ": ");
    }

    int matchesCount = 0;
    matchesCount = searchWord(searchFile, wordToSearch, matchesCount);

    System.out.println("Search completed. Matches found : " + matchesCount);

}

private static int searchWord(File sourceFile, String wordToSearch, int matchesCount) {

    int count = matchesCount;
    if (sourceFile.isDirectory()) {
        String[] sourceFilesArray = sourceFile.list();

        for (String nextFileName : sourceFilesArray) {
            File nextFile = new File(sourceFile.getAbsolutePath() + System.getProperty("file.separator")
                    + nextFileName);
            count = searchWord(nextFile, wordToSearch, count);
        }
    } else {
        try {
            BufferedReader sourceFileReader = new BufferedReader(new FileReader(sourceFile));
            String nextLine = "";
            while (true) {
                nextLine = sourceFileReader.readLine();
                if (null == nextLine) {
                    break;
                }

                if (nextLine.contains(wordToSearch)) {
                    System.out.println("found in -> " + sourceFile.getCanonicalPath());
                    count++;
                }

            }
            sourceFileReader.close();
        } catch (Exception e) {
            System.err.println("Error while reading file " + sourceFile.getAbsolutePath() + " : " + e.getMessage());
        }
    }

    return count;
}

}

I'm just wondering what is a purpose? 我只是想知道目的是什么? personal education? 个人教育?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM