从java中的File中搜索字符串值

时间:2016-12-27 04:28:42

标签: search

以下是存储在File中的值 TASKNAME:描述:状态:优先:会议,高,INPROGRESS,客户端会议

TASKNAME:描述:状态:优先:培训,高,去,JavaTraining

taskname:description:status:priority:面试,高,进度,新面试 taskname:description:status:priority:meeting,low,started,with team

TASKNAME:描述:状态:优先:会议,高,INPROGRESS,队

从这些我需要搜索字符串值,例如:会议 如何在第一次出现时进行搜索并将其返回。

请帮助

1 个答案:

答案 0 :(得分:0)

您可以使用Scanner和string contains方法检查字符串模式。

你可以查看link

import java.io.*;
import java.util.Scanner;

class searchForName
{
public static void main(String[] args) throws FileNotFoundException {
        String name = "meeting";
        try {
        File file = new File("input.txt");
        Scanner kb = new Scanner(System.in);
        final Scanner scanner = new Scanner(file);
        while (scanner.hasNextLine()) {
                final String lineFromFile = scanner.nextLine();
                if(lineFromFile.contains(name)) {
                // a match!
                System.out.println("I found " +name+ " in file " +file.getName());
                break;
        }
        }
        }
        catch (FileNotFoundException ex)
        {
                System.out.println("File not found");
        }
}
}