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 / June 2007

Tip: Looking for answers? Try searching our database.

Parsing a text file format

Thread view: 
JR - 19 Jun 2007 16:18 GMT
I am trying to figure out the code to read in the structure of a text
file to parse it.  The way it works is the first block is the layout,
then the rest has the data so:

--- start text file ---
firstname
lastname
address1
address2
city
state
~~~~~~
firstname=joe
lastname=riley
address1=444 east park lane
address2=NULL
city=Amadam
state=Ohio
~~~~~~
firstname=pete
lastname=johnson
address1=101 east park lane
address2=NULL
city=Aratmas
state=Georgia
--- end text file ---

If someone could help provide me some sample code that could read in
the first block, and store the count and "column" names in an array or
other means, then parse the rest and output them to the screen that
would be good.  I have some manipulation I need to do with the data,
but this would put me in the right direction.

Thanks for any help someone could provide.

JR
Joshua Cranmer - 19 Jun 2007 16:34 GMT
> I am trying to figure out the code to read in the structure of a text
> file to parse it.  The way it works is the first block is the layout,
> then the rest has the data so:

public class TextFileExample {
static String[] readColumns(BufferedReader in) {
   LinkedList<String> columns = new LinkedList<String>();
   String s;
   while (!(s = in.readLine()).equals("~~~~~~")) {
       columns.add(s);   
   }
   return columns.toArray(new String[0]);
}
static String[] readRecord(BufferedReader in, String[] columns) {
   String[] record = new String[columns.length];
   for (int i=0;i<columns.length;i++) {
       record[i] = in.readLine();
       record[i] = record[i].substring(record[i].indexOf('=')+1);
   }
   return record;
}
public static void main(String[] args) throws Exception{
   BufferedReader in = new BufferedReader(new FileReader("test.txt"));
   String[] header = readColumns(in);
   String s;
   do {
       String[] record = readRecord(in, header);
       for (String key : record)
           System.out.println(key);
       System.out.println("------");
   } while ((s = in.readLine()) != null);
}
}

This is not very good code (it assumes that at least one record exists),
but it should give you a starting point.
richardsosborn@gmail.com - 19 Jun 2007 21:24 GMT
> I am trying to figure out the code to read in the structure of a text
> file to parse it.  The way it works is the first block is the layout,
[quoted text clipped - 32 lines]
>
> JR

You might want to play with making your returned object an array of
Chars and not Strings, John. (I.E. char[])
as well.  It's faster.
doug.mcclelland@gmail.com - 19 Jun 2007 21:43 GMT
On Jun 19, 4:24 pm, "richardsosb...@gmail.com"
<richardsosb...@gmail.com> wrote:

> > I am trying to figure out the code to read in the structure of a text
> > file to parse it.  The way it works is the first block is the layout,
[quoted text clipped - 36 lines]
> Chars and not Strings,John. (I.E. char[])
> as well.  It's faster.

I agree. I believe arrays are your best choice from an efficiency
point of view, John.
Roedy Green - 20 Jun 2007 10:59 GMT
>~~~~~~
>firstname=joe
[quoted text clipped - 10 lines]
>city=Aratmas
>state=Georgia

It is not clear exactly what your file contains .  are those ~~
actually in the file or did you just add tem to the post to logically
break up the groups?

In any case you read a line at a time.  See
http://mindprod.com/applets/fileio.html for sample code to do that.
You want buffered characters.

Then you look at the first char to decide if it is a ~~ or data line.

Then you use indexOf to find the =.

Then you use substring to graph the field name and value.  Then you
test the field value, to figure out what sort of a value you have.

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.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.