解析文本文件并搜索特定单词

时间:2011-10-25 16:27:31

标签: java file io

我有以下文本文件,我想知道如何解析它并搜索Cell和Engine单词,我想要的是打印出包含Cell,Engine单词的方法名称。以下是txt文件,不要将其视为java代码,因为我已将其移动到txt文件以进行解析。

@Test
  public void testGetMonsters() {
        Cell cell11 = aBoard.getCell(1, 1);
        theEngine = new Engine(theGame);   
  }

  @Test
  public void testDxDyPossibleMove() {
        Cell cell11 = aBoard.getCell(1, 1);
  }   

所需的解析输出如下:

testGetMonsters class contains Cell and Engine words
testDxDyPossibleMove class contains Cell word

4 个答案:

答案 0 :(得分:1)

您可能希望使用正则表达式和Java的模式匹配工具。请查看Regular Expressions and the Java Programming Language示例用法。

快速举例:

Pattern cellPattern = Pattern.compile("Cell");
    while (fileReader.ready()) {
        String inputLine = fileReader.readLine();
        Matcher cellMatcher = cellPattern.matcher(inputLine);
        if(cellMatcher.lookingAt()) {
            //This line contains the word "Cell"

确定你所在的课程是否属于另一个问题......你需要一个“词法分析器”。 JavaCC是一个很好的起点。

答案 1 :(得分:1)

我没有看到您的代码与您的问题之间的链接,但是:

String text = new Scanner(yourFile).useDelimiter("\\Z").next();
String wordsToLookFor = Arrays.asList("cell", "engine");

List<String> wordsContained = new ArrayList<String>();

for(String word : wordsToLookFor){
  if(text.contains(word)) {
     wordsContained.add(word);
  }
}

System.out.println(yourFile.getName() + " contains " + wordsContained);

答案 2 :(得分:0)

虽然我不确定我理解你的问题是一些提示。

要使用obj.getClass().getName()获取课程名称。 要搜索字符串,请使用str.contains(“Cell”)或使用正则表达式。看看课程java.util.Pattern

答案 3 :(得分:0)

Tadaaaa!

package textsearch;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TextSearch {

    public static void main(final String[] args) throws Exception {

        final URL testFileURL = Thread.currentThread().getContextClassLoader().getResource("textsearch/test.txt");
        final File input = new File(testFileURL.toURI());
        final FileReader reader = new FileReader(input);

        final SearchString search1 = new SearchString("Cell");
        final SearchString search2 = new SearchString("Engine");

        final List<SearchString> searches = new ArrayList<SearchString>();
        searches.add(search1);
        searches.add(search2);

        final Search search = new Search(searches);

        try {
            search.search(reader);
        } finally {
            reader.close();
        }

        if(search.hasPositives()) {
            System.out.print("File " + input.getName() + " contains the words ");
            search.printPositives();
            System.out.println("");
        } else {
            System.out.println("File " + input.getName() + " didn't contain any of the search terms.");
        }

    }

    private static class SearchString {

        final String searchString;
        final ParsePosition pos;

        SearchString(final String searchString) {

            if(searchString == null || searchString.isEmpty())
                throw new IllegalArgumentException("I can't search for nothing!");

            this.searchString = searchString;
            pos = new ParsePosition(0);

        }

        boolean checkNextChar(final char c) {

            if(searchString.charAt(pos.getIndex()) == c) {
                pos.setIndex(pos.getIndex() + 1);
                if(pos.getIndex() >= searchString.length()) {
                    pos.setIndex(0);
                    return true;
                }
                return false;
            } else {
                pos.setIndex(0);
                return false;
            }

        }

        String getString() {

            return searchString;

        }

    }

    private static class Search {

        private final List<SearchString> searches;
        private final List<SearchString> positives;

        Search(final List<SearchString> searches) {

            this.searches = searches;
            positives = new ArrayList<SearchString>();

        }

        void search(final Reader reader) throws IOException {

            int current;

            while((current = reader.read()) != -1 && !searches.isEmpty()) {

                char c = (char)current;

                for(final Iterator<SearchString> it = searches.iterator(); it.hasNext();) {
                    final SearchString searchString = it.next();
                    final boolean matches = searchString.checkNextChar(c);
                    if(matches) {
                        positives.add(searchString);
                        it.remove();
                    }
                }

            }

        }

        boolean hasPositives() {

            return !positives.isEmpty();

        }

        void printPositives() {

            for(final Iterator<SearchString> it = positives.iterator(); it.hasNext();) {
                final SearchString searchString = it.next();
                System.out.print(searchString.getString());
                if(it.hasNext())
                    System.out.print(", ");
            }

        }

    }

}

好吧,它不是100%可靠,但它是一个开始。

编辑:或者您可以使用像KayKay建议的扫描仪,但其中的乐趣在于:D