Stopping and Starting IO

P

printdude1968

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
 
D

Daniel Pitts

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?
 
P

printdude1968

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

Tom Forsmo

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
 
R

RedGrittyBrick

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;

}
 
D

Daniel Pitts

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

printdude1968

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

printdude1968

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)
 
I

Ian Wilson

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
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top