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 / November 2006

Tip: Looking for answers? Try searching our database.

Stopping and Starting IO

Thread view: 
printdude1968@gmail.com - 09 Nov 2006 00:32 GMT
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 - 09 Nov 2006 02:08 GMT
> Hi,
>
[quoted text clipped - 88 lines]
> 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 - 09 Nov 2006 02:52 GMT
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.

> > Hi,
> >
[quoted text clipped - 90 lines]
>
> Why *aren't* you using an XML SAX or pull parser?
Daniel Pitts - 09 Nov 2006 18:45 GMT
> > 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
[quoted text clipped - 3 lines]
> 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 - 10 Nov 2006 01:26 GMT
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.
> > > 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
[quoted text clipped - 10 lines]
> with him/her first, ofcourse).  You will be happier if you learn these
> things.
Tom Forsmo - 09 Nov 2006 11:11 GMT
> 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 - 09 Nov 2006 18:34 GMT
> I have a file that looks like this:
> ...
[quoted text clipped - 13 lines]
> <d2>
> ....

Looks ideally suited to an XML parser.

> java parseControl prod d1 p1

> And what I need for I/O is something like this
>
[quoted text clipped - 4 lines]
> 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;

}
printdude1968@gmail.com - 10 Nov 2006 03:46 GMT
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 have a file that looks like this:
> > ...
[quoted text clipped - 61 lines]
>
> }
Ian Wilson - 10 Nov 2006 14:46 GMT
> 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


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.