搜索文本文件java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29529102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Searching through a text file java
提问by
So I am trying to search through a text file and if the user input is found, it returns the entire sentence including white spaces.But apparently I only get the first string and nothing pass the first string in the sentence. For example if i have a text file called "data.txt" and the contents in the first line is " I am a legend". after user enters "I am a legend" the output after the file is searched is "I". Any help would be appreciated.
所以我试图搜索一个文本文件,如果找到用户输入,它会返回包括空格在内的整个句子。但显然我只得到第一个字符串,而没有传递句子中的第一个字符串。例如,如果我有一个名为“data.txt”的文本文件,第一行的内容是“我是传奇”。用户输入“我是传奇”后,搜索文件后的输出为“我”。任何帮助,将不胜感激。
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());
}
采纳答案by takeradi
package com.example;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileSearch {
public void parseFile(String fileName,String searchStr) throws FileNotFoundException{
Scanner scan = new Scanner(new File(fileName));
while(scan.hasNext()){
String line = scan.nextLine().toLowerCase().toString();
if(line.contains(searchStr)){
System.out.println(line);
}
}
}
public static void main(String[] args) throws FileNotFoundException{
FileSearch fileSearch = new FileSearch();
fileSearch.parseFile("src/main/resources/test.txt", "am");
}
}
test.txt contains:
I am a legend
Hello World
I am Ironman
Output:
i am a legend
i am ironman
The above code does case insensitive search. You should use nextLine() to get the complete line. next() breaks on whitespaces.
上面的代码不区分大小写搜索。您应该使用 nextLine() 来获取完整的行。next() 在空格处中断。
Reference: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
参考:http: //docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()
回答by
Scanner.next();
returns the next caracter instead use Scanner.readLine();
Scanner.next();
返回下一个字符而不是使用 Scanner.readLine();
Edit:
Belive Scanners use .nextLine();
not .readLine();
编辑:Belive 扫描仪.nextLine();
不使用.readLine();
回答by SaurabhJinturkar
When you are scanning your input..
当您扫描输入时..
Scanner kb = new Scanner(System.in);
System.out.println(" enter the content you looking for");
String name = kb.next();
You are accepting only one token. You should accept whole line to be searched as your token using kb.nextLine()
您只接受一种令牌。您应该接受整行作为您的令牌进行搜索kb.nextLine()