Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Extracting strings from text files

Thread view: 
poener - 28 Aug 2006 16:13 GMT
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.
Oliver Wong - 28 Aug 2006 16:50 GMT
> 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
poener - 28 Aug 2006 16:55 GMT
Oliver Wong schrieb:

> > Hello again,
> >
[quoted text clipped - 5 lines]
>     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.
Oliver Wong - 28 Aug 2006 17:52 GMT
> Oliver Wong schrieb:
>
[quoted text clipped - 15 lines]
> 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
Matt Humphrey - 28 Aug 2006 19:45 GMT
> Oliver Wong schrieb:
>
[quoted text clipped - 15 lines]
> 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 matth@ivizNOSPAM.com http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.