Java 在文本文件中搜索字符串
我试图在文本文件中搜索一组关键字 我把它们作为一个字符串,我对Java 在文本文件中搜索字符串,java,Java,我试图在文本文件中搜索一组关键字 我把它们作为一个字符串,我对if语句有一个问题 在这一行: if(s.contains (keywords)) { 我以前没有。它说要做到以下几点: 类型字符串中包含的方法(CharSequence)不适用于参数(字符串[]) 但这只是将字符串更改为CharSequence,仍然会导致错误 代码如下: import java.io.*; public class SearchTextFile { public static void main(St
if
语句有一个问题
在这一行:
if(s.contains (keywords)) {
我以前没有。它说要做到以下几点:
类型字符串中包含的方法(CharSequence)不适用于参数(字符串[])
但这只是将字符串更改为CharSequence
,仍然会导致错误
代码如下:
import java.io.*;
public class SearchTextFile {
public static void main(String args[]) throws Exception {
int tokencount;
FileReader fr = new FileReader("c:\\searchtxt.txt");
BufferedReader br = new BufferedReader(fr);
String s;
int linecount = 0;
String[] keywords = {
"AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"
};
String line;
while ((s = br.readLine()) != null) {
if (s.contains(keywords)) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
}
不能将字符串数组(
String[]
)与contains
一起使用,请改用正则表达式,即:
import java.util.regex.*;
if (subjectString.matches("AB|BCD1|B11|BL:|B/1:|B1L:|B11:")) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
不能将字符串数组(
String[]
)与contains
一起使用,请改用正则表达式,即:
import java.util.regex.*;
if (subjectString.matches("AB|BCD1|B11|BL:|B/1:|B1L:|B11:")) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
一种更有效的方法是将关键字列表存储在一个集合中,并将每行上的令牌列表存储在一个集合中。这可以防止迭代列表中的所有元素时效率低下。考虑到这一点,我稍微修改了您的代码:
public static void main(String args[]) throws Exception
{
int tokencount;
FileReader fr=new FileReader("c:\\searchtxt.txt");
BufferedReader br=new BufferedReader(fr);
String s;
int linecount=0;
//String[] keywords = { "AB", "BC","D1", "B11", "BL:", "B/1:","B1L:", "B11:"};
Set<String> keywords = new HashSet<>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"));
String line;
Set<String> lineSet;
while ((s=br.readLine())!=null) {
lineSet = new HashSet(Arrays.asList(s.split(" ")));
if(!Collections.disjoint(lineSet, keywords)) { //returns true if both sets have no elements in common;
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
publicstaticvoidmain(字符串args[])引发异常
{
整数计数;
FileReader fr=新的FileReader(“c:\\searchtxt.txt”);
BufferedReader br=新的BufferedReader(fr);
字符串s;
int linecount=0;
//字符串[]关键字={“AB”、“BC”、“D1”、“B11”、“BL:”、“B/1:”、“B1L:”、“B11:”};
Set关键字=新的HashSet(Arrays.asList(“AB”、“BC”、“D1”、“B11”、“BL:”、“B/1:”、“B1L:”、“B11:”);
弦线;
设置行集;
而((s=br.readLine())!=null){
lineSet=newhashset(Arrays.asList(s.split(“”));
if(!Collections.disjoint(lineSet,keywords)){//如果两个集合没有共同的元素,则返回true;
系统输出打印项次;
字符串nextLine=br.readLine();
System.out.println(nextLine);
}
}
}
更有效的方法是将关键字列表存储在一个集合中,并将每行上的标记列表存储在一个集合中。这可以防止迭代列表中的所有元素时效率低下。考虑到这一点,我稍微修改了您的代码:
public static void main(String args[]) throws Exception
{
int tokencount;
FileReader fr=new FileReader("c:\\searchtxt.txt");
BufferedReader br=new BufferedReader(fr);
String s;
int linecount=0;
//String[] keywords = { "AB", "BC","D1", "B11", "BL:", "B/1:","B1L:", "B11:"};
Set<String> keywords = new HashSet<>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"));
String line;
Set<String> lineSet;
while ((s=br.readLine())!=null) {
lineSet = new HashSet(Arrays.asList(s.split(" ")));
if(!Collections.disjoint(lineSet, keywords)) { //returns true if both sets have no elements in common;
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
publicstaticvoidmain(字符串args[])引发异常
{
整数计数;
FileReader fr=新的FileReader(“c:\\searchtxt.txt”);
BufferedReader br=新的BufferedReader(fr);
字符串s;
int linecount=0;
//字符串[]关键字={“AB”、“BC”、“D1”、“B11”、“BL:”、“B/1:”、“B1L:”、“B11:”};
Set关键字=新的HashSet(Arrays.asList(“AB”、“BC”、“D1”、“B11”、“BL:”、“B/1:”、“B1L:”、“B11:”);
弦线;
设置行集;
而((s=br.readLine())!=null){
lineSet=newhashset(Arrays.asList(s.split(“”));
if(!Collections.disjoint(lineSet,keywords)){//如果两个集合没有共同的元素,则返回true;
系统输出打印项次;
字符串nextLine=br.readLine();
System.out.println(nextLine);
}
}
}
没有包含(字符串[])的方法。正确的是contains(String)
就像@Pedro Lobito所说的,你可以使用正则表达式。你也可以在字符串数组中循环
while ((s=br.readLine())!=null) {
for (String str: keywords) {
if(s.contains (str)) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
没有包含(字符串[])的方法。正确的是contains(String) 就像@Pedro Lobito所说的,你可以使用正则表达式。你也可以在字符串数组中循环
while ((s=br.readLine())!=null) {
for (String str: keywords) {
if(s.contains (str)) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
由于String没有方法
String.contains(String)
,您可以按如下方式实现它:
将关键字数组更改为ArrayList
。
然后,当您读取一行时,使用String.split()
方法获取数组中的所有单词。现在,您可以循环遍历单词数组(通过读取文件中的行创建),并检查关键字列表是否包含该单词。
注意:KeywordList.contains(s)
如果s
与关键字完全相同,则为true
。如果s
是包含其他单词的字符串,但它包含关键字数组中的一个或多个元素,则会生成false
。因此,此测试不会生成有效的搜索结果。搜索的目的是检查任何输入行s
,如果它至少有一个来自keywords
数组的关键字。因此,其中一种解决方案可以如下所示:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
public class SearchTextFile
{
public static void main(String args[]) throws Exception
{
int tokencount;
FileReader fr = new FileReader("c:\\searchtxt.txt");
BufferedReader br = new BufferedReader(fr);
String s = "";
int linecount = 0;
ArrayList<String> keywordList = new ArrayList<String>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"));
String line;
while ((s = br.readLine()) != null)
{
String[] lineWordList = s.split(" ");
for (String word : lineWordList)
{
if (keywordList.contains(word))
{
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
break;
}
}
}
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.util.ArrayList;
公共类SearchTextFile
{
公共静态void main(字符串args[])引发异常
{
整数计数;
FileReader fr=新的FileReader(“c:\\searchtxt.txt”);
BufferedReader br=新的BufferedReader(fr);
字符串s=“”;
int linecount=0;
ArrayList关键字列表=新的ArrayList(Arrays.asList(“AB”、“BC”、“D1”、“B11”、“BL:”、“B/1:”、“B1L:”、“B11:”);
弦线;
而((s=br.readLine())!=null)
{
字符串[]lineWordList=s.split(“”);
for(字符串字:lineWordList)
{
if(关键字列表.包含(word))
{
系统输出打印项次;
字符串nextLine=br.readLine();
System.out.println(nextLine);
打破
}
}
}
}
}
由于String没有方法String.contains(String)
,您可以按如下方式实现它:
将关键字数组更改为ArrayList
。
然后,当您读取一行时,使用String.split()
方法获取数组中的所有单词。现在,您可以循环遍历单词数组(通过读取文件中的行创建),并检查关键字列表是否包含该单词。
注意:KeywordList.contains(s)
如果s
与关键字完全相同,则为true
。如果s
是包含其他单词的字符串,但它包含关键字数组中的一个或多个元素,则会生成false
。因此,此测试不会生成有效的搜索结果。搜索的目的是检查任何输入行s
,如果它至少有一个