Stopping and Starting IO  
Author Message
printdude1968@gmail.com





PostPosted: 2006-11-9 8:33:00 Top

java-programmer, Stopping and Starting IO Hi,

I have a file that looks like this:

.
.
.
<prod>
<d1>
<p1:configuration>
<p1:f1></p1:f1>
<p1:f2></p1:f2>
<p1:f3></p1:f3>
</p1:configuration>
<p2:configuration>
<p2:f1></p2:f1>
<p2:f2></p2:f2>
<p2:f3></ps:f3>
</p2:configuration>
</d1>
<d2>
.
.
.
.
</d2>
</prod>
<dev>
<d1>
<p1:configuration>
<p1:f1></p1:f1>
<p1:f2></p1:f2>
<p1:f3></p1:f3>
</p1:configuration>
<p2:configuration>
<p2:f1></p2:f1>
<p2:f2></p2:f2>
<p2:f3></ps:f3>
</p2:configuration>
</d1>
<d2>
.
.
.
.
</d2>
</dev>

I know this looks like xml but please bear with me...

Here is my java command

java parseControl prod d1 p1
^ ^ ^
environment_| | |
destination_____ | |
printer_____________|
And what I need for I/O is something like this

Start at the beginning of the file
Search forward until I find <prod>
Now search forward until I find <d1>
Now search forward until I find <p1>
And this is where I start processing until I no longer have a <p1> <--
this is a while loop of some sort?

I have the code written for the main processing loop
while (i still have a <p1> )
{
String testString=s.trim();
pcArrayList.add(testString);
}

In fact my code was doing fine until I introduced the destination piece
(yeah bad upfront design on my part)
Once my pcArrayList is loaded, parsing it for what I need is easy and
works... but I just can't figure out how to load the array based on the
required search criteria.

Is there an easy way to do this?
FileInputStream and FileReader don't seem to lend themselves to
stopping and starting...they seem to be more oriented to a continuous
pass. I could probably do this in 4 passes of the file
1) Read each line and if <proc> then set linecount1 to current line
number (counter)
2) Read each line and ignore all that have a line number <= linecount1
and if not then check for "<d1>"
etc

But this is not very pretty and if I can't do what I need with some
degree of speed and elegance using java, I may as well go back to a
purly ksh and awk implementation which means I've wasted 2 days

 
Daniel Pitts





PostPosted: 2006-11-9 10:08:00 Top

java-programmer >> Stopping and Starting IO
email***@***.com wrote:
> Hi,
>
> I have a file that looks like this:
>
> .
> .
> .
> <prod>
> <d1>
> <p1:configuration>
> <p1:f1></p1:f1>
> <p1:f2></p1:f2>
> <p1:f3></p1:f3>
> </p1:configuration>
> <p2:configuration>
> <p2:f1></p2:f1>
> <p2:f2></p2:f2>
> <p2:f3></ps:f3>
> </p2:configuration>
> </d1>
> <d2>
> .
> .
> .
> .
> </d2>
> </prod>
> <dev>
> <d1>
> <p1:configuration>
> <p1:f1></p1:f1>
> <p1:f2></p1:f2>
> <p1:f3></p1:f3>
> </p1:configuration>
> <p2:configuration>
> <p2:f1></p2:f1>
> <p2:f2></p2:f2>
> <p2:f3></ps:f3>
> </p2:configuration>
> </d1>
> <d2>
> .
> .
> .
> .
> </d2>
> </dev>
>
> I know this looks like xml but please bear with me...
>
> Here is my java command
>
> java parseControl prod d1 p1
> ^ ^ ^
> environment_| | |
> destination_____ | |
> printer_____________|
> And what I need for I/O is something like this
>
> Start at the beginning of the file
> Search forward until I find <prod>
> Now search forward until I find <d1>
> Now search forward until I find <p1>
> And this is where I start processing until I no longer have a <p1> <--
> this is a while loop of some sort?
>
> I have the code written for the main processing loop
> while (i still have a <p1> )
> {
> String testString=s.trim();
> pcArrayList.add(testString);
> }
>
> In fact my code was doing fine until I introduced the destination piece
> (yeah bad upfront design on my part)
> Once my pcArrayList is loaded, parsing it for what I need is easy and
> works... but I just can't figure out how to load the array based on the
> required search criteria.
>
> Is there an easy way to do this?
> FileInputStream and FileReader don't seem to lend themselves to
> stopping and starting...they seem to be more oriented to a continuous
> pass. I could probably do this in 4 passes of the file
> 1) Read each line and if <proc> then set linecount1 to current line
> number (counter)
> 2) Read each line and ignore all that have a line number <= linecount1
> and if not then check for "<d1>"
> etc
>
> But this is not very pretty and if I can't do what I need with some
> degree of speed and elegance using java, I may as well go back to a
> purly ksh and awk implementation which means I've wasted 2 days

Why *aren't* you using an XML SAX or pull parser?

 
printdude1968@gmail.com





PostPosted: 2006-11-9 10:53:00 Top

java-programmer >> Stopping and Starting IO I used a canned Xerses program to test the well-formed-ness and to show
me a DOM tree, but I haven't been able to find any examples of how to
do exactly what I need to do and I really don't have enough time to
learn the whole thing... but I was able to make my program work by
reading the entire XML file into an ArrayList and then I do one
iteration through the whole thing.

Daniel Pitts wrote:
> email***@***.com wrote:
> > Hi,
> >
> > I have a file that looks like this:
> >
> > .
> > .
> > .
> > <prod>
> > <d1>
> > <p1:configuration>
> > <p1:f1></p1:f1>
> > <p1:f2></p1:f2>
> > <p1:f3></p1:f3>
> > </p1:configuration>
> > <p2:configuration>
> > <p2:f1></p2:f1>
> > <p2:f2></p2:f2>
> > <p2:f3></ps:f3>
> > </p2:configuration>
> > </d1>
> > <d2>
> > .
> > .
> > .
> > .
> > </d2>
> > </prod>
> > <dev>
> > <d1>
> > <p1:configuration>
> > <p1:f1></p1:f1>
> > <p1:f2></p1:f2>
> > <p1:f3></p1:f3>
> > </p1:configuration>
> > <p2:configuration>
> > <p2:f1></p2:f1>
> > <p2:f2></p2:f2>
> > <p2:f3></ps:f3>
> > </p2:configuration>
> > </d1>
> > <d2>
> > .
> > .
> > .
> > .
> > </d2>
> > </dev>
> >
> > I know this looks like xml but please bear with me...
> >
> > Here is my java command
> >
> > java parseControl prod d1 p1
> > ^ ^ ^
> > environment_| | |
> > destination_____ | |
> > printer_____________|
> > And what I need for I/O is something like this
> >
> > Start at the beginning of the file
> > Search forward until I find <prod>
> > Now search forward until I find <d1>
> > Now search forward until I find <p1>
> > And this is where I start processing until I no longer have a <p1> <--
> > this is a while loop of some sort?
> >
> > I have the code written for the main processing loop
> > while (i still have a <p1> )
> > {
> > String testString=s.trim();
> > pcArrayList.add(testString);
> > }
> >
> > In fact my code was doing fine until I introduced the destination piece
> > (yeah bad upfront design on my part)
> > Once my pcArrayList is loaded, parsing it for what I need is easy and
> > works... but I just can't figure out how to load the array based on the
> > required search criteria.
> >
> > Is there an easy way to do this?
> > FileInputStream and FileReader don't seem to lend themselves to
> > stopping and starting...they seem to be more oriented to a continuous
> > pass. I could probably do this in 4 passes of the file
> > 1) Read each line and if <proc> then set linecount1 to current line
> > number (counter)
> > 2) Read each line and ignore all that have a line number <= linecount1
> > and if not then check for "<d1>"
> > etc
> >
> > But this is not very pretty and if I can't do what I need with some
> > degree of speed and elegance using java, I may as well go back to a
> > purly ksh and awk implementation which means I've wasted 2 days
>
> Why *aren't* you using an XML SAX or pull parser?

 
 
Tom Forsmo





PostPosted: 2006-11-9 19:12:00 Top

java-programmer >> Stopping and Starting IO

email***@***.com wrote:
> Is there an easy way to do this?
> FileInputStream and FileReader don't seem to lend themselves to
> stopping and starting...they seem to be more oriented to a continuous
> pass. I could probably do this in 4 passes of the file

Why don't you use RandomAccessFile, you can read and write natives and
text lines along with seeking within the file.

I am not sure what you mean by start and stop, but I am assuming you
mean stop and then start from a previous point again or skip forward and
similar.

tom
 
 
RedGrittyBrick





PostPosted: 2006-11-10 2:34:00 Top

java-programmer >> Stopping and Starting IO email***@***.com wrote:
> I have a file that looks like this:
> ...
> <prod>
> <d1>
> <p1:configuration>
> <p1:f1></p1:f1>
> <p1:f2></p1:f2>
> <p1:f3></p1:f3>
> </p1:configuration>
> <p2:configuration>
> <p2:f1></p2:f1>
> <p2:f2></p2:f2>
> <p2:f3></ps:f3>
> </p2:configuration>
> </d1>
> <d2>
> ....

Looks ideally suited to an XML parser.

> java parseControl prod d1 p1

> And what I need for I/O is something like this
>
> Start at the beginning of the file
> Search forward until I find <prod>
> Now search forward until I find <d1>
> Now search forward until I find <p1>
> And this is where I start processing until I no longer have a <p1> <--
> this is a while loop of some sort?

You could do something like ... (pseudocode)

boolean withinEnvironment = false;
boolean withinDestination = false;
boolean withinPrinter = false;

while (read another line) {

if (line matches specified environment end tag)
withinEnvironment = false;

if (withinEnvironment) {

if (line matches dest end tag)
withinDestination = false;

if (withinDestination) {

... etc for printer

if (withinPrinter)
MyArrayList.add(line);

} // destination

if (line matches dest start tag)
withinDestination = true;

} // environment

if (line matches specified environment start tag)
withinEnvironment = true;

}


 
 
Daniel Pitts





PostPosted: 2006-11-10 2:46:00 Top

java-programmer >> Stopping and Starting IO
email***@***.com wrote:
> Daniel Pitts wrote:
> > Why *aren't* you using an XML SAX or pull parser?
> I used a canned Xerses program to test the well-formed-ness and to show
> me a DOM tree, but I haven't been able to find any examples of how to
> do exactly what I need to do and I really don't have enough time to
> learn the whole thing... but I was able to make my program work by
> reading the entire XML file into an ArrayList and then I do one
> iteration through the whole thing.
>

Trust me, using an existing parser (SAX or a pull parser would probably
do best for you) is a lot easier than parsing it yourself.

Pick up a book about SAX in Java, and expense it to your boss. (Check
with him/her first, ofcourse). You will be happier if you learn these
things.

 
 
printdude1968@gmail.com





PostPosted: 2006-11-10 9:26:00 Top

java-programmer >> Stopping and Starting IO I have a book called Pro Apache XML.. it has some information in it...
I heard about a product called J...something or other that will perform
the functions I need.
Daniel Pitts wrote:
> email***@***.com wrote:
> > Daniel Pitts wrote:
> > > Why *aren't* you using an XML SAX or pull parser?
> > I used a canned Xerses program to test the well-formed-ness and to show
> > me a DOM tree, but I haven't been able to find any examples of how to
> > do exactly what I need to do and I really don't have enough time to
> > learn the whole thing... but I was able to make my program work by
> > reading the entire XML file into an ArrayList and then I do one
> > iteration through the whole thing.
> >
>
> Trust me, using an existing parser (SAX or a pull parser would probably
> do best for you) is a lot easier than parsing it yourself.
>
> Pick up a book about SAX in Java, and expense it to your boss. (Check
> with him/her first, ofcourse). You will be happier if you learn these
> things.

 
 
printdude1968@gmail.com





PostPosted: 2006-11-10 11:47:00 Top

java-programmer >> Stopping and Starting IO Ok, that looks like it will work for my purpose, but what is the java
class/method I would need to use to implement:

while (read another line)


RedGrittyBrick wrote:
> email***@***.com wrote:
> > I have a file that looks like this:
> > ...
> > <prod>
> > <d1>
> > <p1:configuration>
> > <p1:f1></p1:f1>
> > <p1:f2></p1:f2>
> > <p1:f3></p1:f3>
> > </p1:configuration>
> > <p2:configuration>
> > <p2:f1></p2:f1>
> > <p2:f2></p2:f2>
> > <p2:f3></ps:f3>
> > </p2:configuration>
> > </d1>
> > <d2>
> > ....
>
> Looks ideally suited to an XML parser.
>
> > java parseControl prod d1 p1
>
> > And what I need for I/O is something like this
> >
> > Start at the beginning of the file
> > Search forward until I find <prod>
> > Now search forward until I find <d1>
> > Now search forward until I find <p1>
> > And this is where I start processing until I no longer have a <p1> <--
> > this is a while loop of some sort?
>
> You could do something like ... (pseudocode)
>
> boolean withinEnvironment = false;
> boolean withinDestination = false;
> boolean withinPrinter = false;
>
> while (read another line) {
>
> if (line matches specified environment end tag)
> withinEnvironment = false;
>
> if (withinEnvironment) {
>
> if (line matches dest end tag)
> withinDestination = false;
>
> if (withinDestination) {
>
> ... etc for printer
>
> if (withinPrinter)
> MyArrayList.add(line);
>
> } // destination
>
> if (line matches dest start tag)
> withinDestination = true;
>
> } // environment
>
> if (line matches specified environment start tag)
> withinEnvironment = true;
>
> }

 
 
Ian Wilson





PostPosted: 2006-11-10 22:46:00 Top

java-programmer >> Stopping and Starting IO email***@***.com wrote:
> Ok, that looks like it will work for my purpose, but what is the java
> class/method I would need to use to implement:
>
> while (read another line)
>

One way might be

BufferedReader in = new BufferedReader(new FileReader("foo.xml"));
String line;
while((line = in.readLine()) != null) {
// process line
}

See http://javadocs.org/BufferedReader