我正在尝试搜索一个文本文件,如果找到用户输入的内容,就返回整个句子(包括空格)。但显然我只得到了第一个字符串,而且在句子中没有其他内容。例如,如果我有一个名为"data.txt"的文本文件,第一行的内容是"我是传奇"。当用户输入"I am a legend"后,搜索文件后的输出是"I"。任何帮助都将不胜感激。
public static void Findstr() { // This function searches the text for the string
File file = new File("data.txt");
Scanner kb = new Scanner(System.in);
System.out.println(" enter the content you looking for");
String name = kb.next();
Scanner scanner;
try {
scanner = new Scanner(file).useDelimiter( ",");
while (scanner.hasNext()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(name)) {
// a match!
System.out.println("I found " + name);
break;
}
}
} catch (IOException e) {
System.out.println(" cannot write to file " + file.toString());
}