Manu
Manu

Reputation: 13

Reading a set of values from a text file

{  
   "TEST":"189456",
   "TEST1":"X_Y_Z",
   "TEST2":"Y_Z_W",
   "TEST3":"GGG  ",
   "TEST4":"32423423233322"
},
{  
   "TEST":"123456",
   "TEST1":"X_E_Z",
   "TEST2":"T_Z_W",
   "TEST3":"EWE ",
   "TEST4":"324234243234"
}

This is a .txt file I want to read and print only 189456,123456 from the above file.Can anyone help me in doing this.Please find the code for reference.Please post the easiest code.....

  Pattern p = Pattern.compile("\"Test\"\\s*:\\s*\"(.*)\"", Pattern.CASE_INSENSITIVE);

        while ( (line = bf.readLine()) != null) {
            linecount++;

            Matcher m = p.matcher(line);

            // indicate all matches on the line
            while (m.find()) {

                 System.out.println(m.group(1));
            }
        }

Upvotes: 1

Views: 105

Answers (5)

Bala.. Boppudi
Bala.. Boppudi

Reputation: 1

    FileInputStream fstream = new FileInputStream("D:\\prac\\src\\test.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

    String strLine;
    while ((strLine = br.readLine()) != null)   {
        if(strLine.contains("\"TEST\":")){
            System.out.println(strLine.split(":")[1].replaceAll("\"","").replace(",",""));
        }
    }

    br.close();
}

Output:

189456
123456

Upvotes: 0

Minh
Minh

Reputation: 424

With provided input, you should read it as json instead of raw text.

com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
        List<TestObj> test = new ArrayList<TestObj>();
        test = mapper.readValue(new File("c:\\YourFile.txt"), test.getClass());

Where TestObj is something like this:

class TestObj {
    String test;
    String test1;   // You should use json annotation here because it does not match your json field name.
    ...
    // getter setter methods
}

Upvotes: 1

alovaros
alovaros

Reputation: 476

Hope I understood the question the right way :D

String saveData;
Pattern p = Pattern.compile("\"Test\"\\s*:\\s*\"(.*)\"", Pattern.CASE_INSENSITIVE);

while ( (line = bf.readLine()) != null) {
    linecount++;

    Matcher m = p.matcher(line);

    // indicate all matches on the line
    if(line.contains("189456") || line.contains("123456"))  {
        saveData = line;
    }    
}

if the String you get from readLine() contains the searched string it will save it in saveData

Upvotes: 0

Marco A. Hernandez
Marco A. Hernandez

Reputation: 821

Another way to do it:

while ((line = br.readLine()) != null) {
    if(line.contains("\"TEST:\"")){
        String[] lineValues = line.split(":");
        System.out.println(lineValues[1].replace("\"", "").replace(",",""));        
    }     
}

Upvotes: 2

Rambler
Rambler

Reputation: 5482

As for a Regex solution :

(.*)\"TEST":\"(.*?)\"

Note the ? , it makes your regex to stop at the first match of ".

With spaces in between :

(.*)\"TEST"\s*:\s*\"(.*?)\"

Upvotes: 1

Related Questions