user5455037
user5455037

Reputation:

Search in a text file (with specific pattern)

The text file looks something like this:

    keyword12x:
    =========
    Acon:
    a1
    x2
    z3
    Bcon:
    c1
    e2
    w3
    r4

and so on... (always the same sheme and a lot of keywords in the file)

I need a function that I can pass keyword12x and the signal type that I'm looking for which searches through the text file and returns the corresponding Acon or Bcon singnals

Exaple declaration:

public string searchKey(string keyword, string type){
....
}

a call like:

search("keyword12x", "Acon")

outputs:

a1
x2
z3

(type = Bcon would obviously give c1, e2, w3, r4)

EDIT: This is what "I have".. (It's not what I want and only for testing porpose) As you can see it's searching the "keyword12x" line and I'm stuck there.

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:

> step by step "in english":
> 1. go trough lines
> 2. untill you find "keyword12x"
> 3. keep going through lines (from that point !)
> 4. find "Acon:"
> 5. go next line
> 6. start printing out and go next line (loop)
> 7. until line = "Bcon:" appears
> 8. go next line
> 9. start printing out and go next line (loop)
> 10. untill an empty line appears

EDIT3: Lets resume: I want to search for a line containing the keyword (with a ':' appended), then (after that line) search for a line containing the given type (also followed by ':') and then gather all following lines up to, but not including a line that ends with ':' or empty line. [summary by @Carlos Heuberger]

Upvotes: 1

Views: 105

Answers (1)

Poonam Patel
Poonam Patel

Reputation: 73

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();
    }
}
}

try this one.

Upvotes: 2

Related Questions