0

I have a text file which contains data in one line, and I want to extract words from the text file.

The words I want to extract are: "id" and "token"

With Java I can read the file:

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

public class ReadStringFromFile
{
    public static void main(String[] args) throws IOException
    {
        File file = new File("test.txt");
        String string = FileUtils.readFileToString(file);
        System.out.println("Read in: " + string);
    }
}

As the text file is in one line, I do not know how I can extract a value from the String.

Clashsoft
  • 11,553
  • 5
  • 40
  • 79
  • 2
    it's just a string. you'd use string operations to slice 'n dice that text into whatever you want. – Marc B Nov 24 '15 at 20:49
  • provide sample of text file content – michaldo Nov 24 '15 at 20:49
  • You should treat the line as a string once parsed. If a string has id and token, how would you extract? (also what's the format of the line?) – tanjir Nov 24 '15 at 20:49
  • Before `System.out.println(...` type `string.` and then press ctrl+space. Your IDE will show you a list of wonderful things that you can do with your string. Pick something. Try it. See what you get. – Mike Nakis Nov 24 '15 at 20:51
  • the line is like this: "updated_by": null, "created_at": "2012-10-21T19:51:07Z", "updated_at": "2010-10-20T19:51:07Z", "created_by": null, "targets": {}, "publication_start_timestamp": null, "type": "alert", "id": "112-c521-4c-999d-4522f2b", "bodies": {}}, {"default_lang": "fr", – parisFoxparis Nov 24 '15 at 20:57
  • 3
    You need to get familiar with [edit] option. – Pshemo Nov 24 '15 at 20:59
  • If i try with String.valueOf("id"); it displays the whole line – parisFoxparis Nov 24 '15 at 21:06
  • 1
    Will you be looking to expand this further to use the other metadata? Or will you only be needing to use `id` and `token`? Also, would you post in your original question a full example of what the whole line contains? It is cut off in your comment. – DagdA Nov 24 '15 at 21:45

2 Answers2

0

You need to split the string.

In your case I assume the words are separated by a whitespace so string.split("\\s+"); should to the trick.

Community
  • 1
  • 1
mariosangiorgio
  • 5,520
  • 4
  • 32
  • 46
0

It looks like you're trying to parse some json code. You could use a json parser (check out: http://www.json.org/java/) or if your needs are simple use a regex to extract the bits you want. Maybe something like:

    File file = new File("test.txt");
    String string = FileUtils.readFileToString(file);
    Pattern re = Pattern.compile("(?:,|\\{)?\"([^:]*)\":(\"[^\"]*\"|\\{[^}]*\\}|[^},]*}?)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
    Matcher m = re.matcher(string);

    // Create a map of all values
    Map<String, String> map = new HashMap<String, String>();
    String id = "NOT_FOUND";
    String token = "NOT_FOUND";
    while (m.find()) {
        map.put(m.group(1), m.group(2).replace("\"", ""));
        if (m.group(1).trim().equals("id")) {
            id = m.group(2).replace("\"", "");
        }
        if (m.group(1).equals("token")) {
            token = m.group(2).replace("\"", "");
        }
    }

    System.out.println("id = " + id + " : token = " + token);

    // or 
    System.out.println(map);
rainkinz
  • 10,082
  • 5
  • 45
  • 73