Stopping and Starting IO

Discussion in 'Java' started by printdude1968@gmail.com, Nov 9, 2006.

  1. Guest

    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
    , Nov 9, 2006
    #1
    1. Advertising

  2. Daniel Pitts Guest

    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?
    Daniel Pitts, Nov 9, 2006
    #2
    1. Advertising

  3. Guest

    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:
    > 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?
    , Nov 9, 2006
    #3
  4. Tom Forsmo Guest

    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
    Tom Forsmo, Nov 9, 2006
    #4
  5. 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;

    }
    RedGrittyBrick, Nov 9, 2006
    #5
  6. Daniel Pitts Guest

    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.
    Daniel Pitts, Nov 9, 2006
    #6
  7. Guest

    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:
    > 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.
    , Nov 10, 2006
    #7
  8. Guest

    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:
    > 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;
    >
    > }
    , Nov 10, 2006
    #8
  9. Ian Wilson Guest

    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
    Ian Wilson, Nov 10, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rhino
    Replies:
    0
    Views:
    1,466
    Rhino
    Nov 15, 2003
  2. Paul Hampton
    Replies:
    0
    Views:
    339
    Paul Hampton
    Jul 3, 2003
  3. dfaber
    Replies:
    1
    Views:
    355
    Amit Khemka
    Jul 4, 2006
  4. -intl.com
    Replies:
    1
    Views:
    358
    Martin Gregorie
    Oct 22, 2006
  5. DevNull
    Replies:
    3
    Views:
    416
Loading...

Share This Page