[英]Java search text file for string and replace or remove a character within the string
[英]Search string within a text file
我正在嘗試在文本文件中搜索一組關鍵字。
我將它們作為字符串放置,並且if
語句出現問題。
在這行上:
if(s.contains (keywords)) {
我以前沒有 它說要執行以下操作:
字符串類型中的方法contains(CharSequence)不適用於參數(String []))
但這只是將String更改為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沒有方法String.contains(String)
,因此可以按以下方式實現它:
將關鍵字數組更改為ArrayList<String>
。 然后,當您讀一行時,使用String.split()
方法獲取數組中的所有單詞。 現在,您可以遍歷單詞數組(通過從文件中讀取行來創建),並檢查keywordList是否包含單詞。 注意:如果s
與關鍵字完全相同,則KeywordList.contains(s)
將為true
。 如果s
是包含其他單詞的字符串,但它包含keywords
array中的一個或多個元素,則它將生成false
s
因此,此測試將不會產生有效的搜索結果。 搜索的目的是檢查任何輸入行s
是否具有keywords
數組中的至少一個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;
}
}
}
}
}
一種更有效的方法是將關鍵字列表存儲在集合中 ,還將標記列表存儲在集合中的每一行上。 這樣可以避免遍歷列表中所有元素的效率低下。 考慮到這一點,我對您的代碼進行了一些修改:
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);
}
}
}
您不能將String數組( 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);
}
沒有contains(String [])這樣的方法。 正確的是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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.