Extracting strings from text files

P

poener

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.
 
O

Oliver Wong

poener said:
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?

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

- Oliver
 
P

poener

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

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.
 
O

Oliver Wong

poener said:
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).

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
 
M

Matt Humphrey

poener said:
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).

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 (e-mail address removed) http://www.iviz.com/
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top