Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Stopping and Starting IO

Reply
Thread Tools

Stopping and Starting IO

 
 
printdude1968@gmail.com
Guest
Posts: n/a
 
      11-09-2006
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

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      11-09-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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?

 
Reply With Quote
 
 
 
 
printdude1968@gmail.com
Guest
Posts: n/a
 
      11-09-2006
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:
> (E-Mail Removed) 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?


 
Reply With Quote
 
Tom Forsmo
Guest
Posts: n/a
 
      11-09-2006


(E-Mail Removed) 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
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      11-09-2006
(E-Mail Removed) 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;

}


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      11-09-2006

(E-Mail Removed) 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.

 
Reply With Quote
 
printdude1968@gmail.com
Guest
Posts: n/a
 
      11-10-2006
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:
> (E-Mail Removed) 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.


 
Reply With Quote
 
printdude1968@gmail.com
Guest
Posts: n/a
 
      11-10-2006
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:
> (E-Mail Removed) 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;
>
> }


 
Reply With Quote
 
Ian Wilson
Guest
Posts: n/a
 
      11-10-2006
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM
Starting and Stopping an Animated GIF Rhino Java 0 11-15-2003 02:48 PM
Starting and stopping a background process Paul Hampton Python 0 07-03-2003 09:58 AM



Advertisments