Java 在文本文件中搜索(使用特定模式)
文本文件如下所示:Java 在文本文件中搜索(使用特定模式),java,search,text,Java,Search,Text,文本文件如下所示: keyword12x: ========= Acon: a1 x2 z3 Bcon: c1 e2 w3 r4 等等。。。(文件中总是相同的sheme和许多关键字) 我需要一个函数,我可以传递关键字12x和我正在寻找的信号类型,它搜索文本文件并返回相应的Acon或Bcon信号 例如声明: public string searchKey(string keyword, string type
keyword12x:
=========
Acon:
a1
x2
z3
Bcon:
c1
e2
w3
r4
等等。。。(文件中总是相同的sheme和许多关键字)
我需要一个函数,我可以传递关键字12x和我正在寻找的信号类型,它搜索文本文件并返回相应的Acon或Bcon信号
例如声明:
public string searchKey(string keyword, string type){
....
}
像这样的电话:
search("keyword12x", "Acon")
产出:
a1
x2
z3
(类型=Bcon显然会给出c1、e2、w3、r4)
编辑:这是“我拥有的”。。(这不是我想要的,只是为了测试porpose)
正如你所看到的,它正在搜索“关键字12x”行,我被卡在那里了
import java.io.File;
import java.util.Scanner;
public class ReadText
{
public static void main(String[] args) throws Exception
{
File file =
new File("C:\\test.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()){
String line = sc.nextLine();
if(line.contains("keyword12x")){
System.out.println(line);
}
}
}
}
EDIT2:
EDIT3:
让我们继续:我要搜索包含关键字的行(附加“:”),然后(在该行之后)搜索包含给定类型的行(后跟“:”),然后收集以下所有行,直到,但不包括以“:”或空行结尾的行。[由@Carlos Heuberger撰写的摘要]
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo
{
static String readLine = "";
String ch ;
static File f = new File("/home/admin1/demoEx");
public String searchKey(String keyword, String type) throws IOException
{
ch = type.toLowerCase().substring(0, 1);
BufferedReader b = null;
try {
b = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Reading file using Buffered Reader");
while ((readLine = b.readLine()) != null)
{
if(readLine.contains(ch))
{
System.out.println(readLine);
}
}
return readLine;
}
public static void main(String args[])
{
try {
String x = (new Demo()).searchKey("keyword12x","Bcon");
} catch (IOException e) {
e.printStackTrace();
}
}
}
试试这一个。那么你到底是粘在哪个零件上的?你试过自己写这个方法吗?是的,尝试了很多次,但我总是被困在这样一个事实上,我只能找到真正的一行“关键字12x”。我不知道如何使用行号或其他(?)跳转到相应的信号告诉我们你已经尝试过什么我们不会做你的家庭作业答案是:你不能。你必须检查每一行。或者试着把信号的行号存储在某个地方,跳过X行直接转到它。只是没想到它会有任何用处,因为我觉得我必须坚持这个尝试…谢谢,我看到你在那里做了什么,实际上我已经看了缓冲读取,因为性能优势,但这仍然没有解决我的主要问题,因为它实际上没有使用关键字。遗憾的是,检查第一个字母对我的真实文件也不起作用。我必须以某种方式实现它,就像我在原始帖子的EDIT2中写下的那样。。。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo
{
static String readLine = "";
String ch ;
static File f = new File("/home/admin1/demoEx");
public String searchKey(String keyword, String type) throws IOException
{
ch = type.toLowerCase().substring(0, 1);
BufferedReader b = null;
try {
b = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Reading file using Buffered Reader");
while ((readLine = b.readLine()) != null)
{
if(readLine.contains(ch))
{
System.out.println(readLine);
}
}
return readLine;
}
public static void main(String args[])
{
try {
String x = (new Demo()).searchKey("keyword12x","Bcon");
} catch (IOException e) {
e.printStackTrace();
}
}
}