Parsing a text file format

J

JR

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
 
J

Joshua Cranmer

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 = in.readLine();
record = record.substring(record.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.
 
R

richardsosborn

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

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

doug.mcclelland

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.

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.

I agree. I believe arrays are your best choice from an efficiency
point of view, John.
 
R

Roedy Green

~~~~~~
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

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.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top