Extracting strings from text files

Discussion in 'Java' started by poener, Aug 28, 2006.

  1. poener

    poener Guest

    Hello again,

    I want to to read lines from a text file and assign each column of each
    line to a different variable, something like read -u FileHandler
    STRING1 STRING2 ... STRINGN in bash... How could this be done when
    programming with java?

    Thanks,

    - J.
     
    poener, Aug 28, 2006
    #1
    1. Advertisements

  2. poener

    Oliver Wong Guest

    For clarification, if your textfile has 80 columns and 1000 lines, you'd
    end up with 80000 variables. Is this correct?

    - Oliver
     
    Oliver Wong, Aug 28, 2006
    #2
    1. Advertisements

  3. poener

    poener Guest

    Exactly.

    In my application I have a file which acts as a lookup table with two
    columns, indicating the id of a device and its address. My class
    receives as arguments the device id(s) (one or several) and looks for
    them in the lookup table, returning the device address(es).

    - J.
     
    poener, Aug 28, 2006
    #3
  4. poener

    Oliver Wong Guest

    Assuming the text file is small enough to fit in memory and that you're
    using only characters in the basic multilingual plane (e.g. no Ancient
    Sumerian characters), the way I'd do this is to read in every line as a
    String and keep them in an array of Strings. Then, for each line, I'd use
    the charat() method to extract the character at a specific column within
    that line.

    - Oliver
     
    Oliver Wong, Aug 28, 2006
    #4
  5. Your column values are really made of strings that represent device id and
    address, right? Something like:

    DEV-1 63412
    DEV-23 45453

    Given the device id you want the address. If that's the case, I suggest a
    simple reader to read the file, separate the columns by the space and put
    the values into a Map indexed by the id. Are there more than 2 columns? Is
    the separator always a space?

    If it's just what's laid out above, it's really as simple as this (not
    compiled or tested, sorely lacking in error handling.)

    Map lookupTable = new HashMap ();
    BufferedReader br = new BufferedReader (new FileReader (...));
    String line = null;
    while ((line = br.readLine()) != null) {
    int p = line.indexOf (" ");
    if (p > 0) {
    String deviceName = line.substring (0, p);
    String address = line.substring (p + 1);
    lookupTable.put (deviceName, address);
    }
    }
    br.close ();

    If you have more columns there are more sophisticated ways to pick off the
    values, such as using regular expressions (which can be good for handling
    less perfect data). It really depends on what you want to do with the
    values. If you really just want them as column values, that's possible too:

    List rows = new ArrayList ();
    BufferedReader br = new BufferedReader (new FileReader (...));
    String line = null;
    while ((line = br.readLine()) != null) {
    String [] row = line.split (" ");
    rows.add (row);
    }
    br.close ();

    You can pick off any row from the list as an array of strings and pick off
    the column value starting with 0 for the first column.

    Matt Humphrey http://www.iviz.com/
     
    Matt Humphrey, Aug 28, 2006
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.