file.close() while running

Discussion in 'Java' started by (-Peter-), May 4, 2008.

  1. (-Peter-)

    (-Peter-) Guest

    Hi..

    I'm currently simulating some physics with a java program.

    The results is printed to a file line by line.

    The program takes several hours to run, so I would like to look at the
    output as soon as it has been calculated.

    Can I do something so that the files closes after each cycle in the
    program, so that I'm able to read it?

    /Peter
     
    (-Peter-), May 4, 2008
    #1
    1. Advertising

  2. (-Peter-)

    Christian Guest

    (-Peter-) schrieb:
    > Hi..
    >
    > I'm currently simulating some physics with a java program.
    >
    > The results is printed to a file line by line.
    >
    > The program takes several hours to run, so I would like to look at the
    > output as soon as it has been calculated.
    >
    > Can I do something so that the files closes after each cycle in the
    > program, so that I'm able to read it?
    >
    > /Peter


    you can open a file and read it while it is being written to..
    What you probably want is calling flush()/force() on your
    stream/FileChannel so anything still cached is written out so you
    actually can see it with other programs.
     
    Christian, May 4, 2008
    #2
    1. Advertising

  3. (-Peter-)

    Arne Vajhøj Guest

    (-Peter-) wrote:
    > I'm currently simulating some physics with a java program.
    >
    > The results is printed to a file line by line.
    >
    > The program takes several hours to run, so I would like to look at the
    > output as soon as it has been calculated.
    >
    > Can I do something so that the files closes after each cycle in the
    > program, so that I'm able to read it?



    On some platform you may be able to read it using simply
    by having your program call flush frequently and then
    view the file the usual way.

    If you need to do it portable I believe you would have to
    close the file one file and open another file.

    Arne
     
    Arne Vajhøj, May 4, 2008
    #3
  4. (-Peter-)

    (-Peter-) Guest

    On 4 Maj, 02:09, Christian <> wrote:
    > (-Peter-) schrieb:
    >
    > > Hi..

    >
    > > I'm currently simulating some physics with a java program.

    >
    > > The results is printed to a file line by line.

    >
    > > The program takes several hours to run, so I would like to look at the
    > > output as soon as it has been calculated.

    >
    > > Can I do something so that the files closes after each cycle in the
    > > program, so that I'm able to read it?

    >
    > > /Peter

    >
    > you can open a file and read it while it is being written to..
    > What you probably want is calling flush()/force() on your
    > stream/FileChannel so anything still cached is written out so you
    > actually can see it with other programs.


    can you explain how to actually do this?

    /peter
     
    (-Peter-), May 4, 2008
    #4
  5. Just when you are finished writing just close the file. But when you
    go to write again use a Random access file to append the new data. Or
    you could use one of those streams that I can't remember the name of
    that acts like one stream but copies the data into two so you could
    give it a FileOutputStream to your file and one to System.out so the
    console would be an exact copy of your file. I prefer the second one.
     
    Chase Preuninger, May 4, 2008
    #5
  6. (-Peter-)

    Roedy Green Guest

    On Sat, 3 May 2008 17:02:43 -0700 (PDT), "(-Peter-)"
    <> wrote, quoted or indirectly quoted someone who
    said :

    >Can I do something so that the files closes after each cycle in the
    >program, so that I'm able to read it?


    you can use flush, but the file stays open. You could output to the
    console.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, May 4, 2008
    #6
  7. (-Peter-)

    BTDTGTTS Guest

    (-Peter-) wrote:

    > Hi..
    >
    > I'm currently simulating some physics with a java program.
    >
    > The results is printed to a file line by line.
    >
    > The program takes several hours to run, so I would like to look at the
    > output as soon as it has been calculated.
    >


    Can you not deal with this through your OS? Under linux try the "tail"
    command. Don't know about Windows though, but you can download some unix
    utilities that have been complied for windows e.g
    http://www.tailforwin32.sourceforge.net

    HTH
     
    BTDTGTTS, May 4, 2008
    #7
  8. Lew wrote:
    > (-Peter-) wrote:
    >>> The results is printed to a file line by line.
    >>>
    >>> The program takes several hours to run, so I would like to look at
    >>> the output as soon as it has been calculated.

    >
    > BTDTGTTS wrote:
    >> Can you not deal with this through your OS? Under linux try the
    >> "tail" command. Don't know about Windows though, but you can
    >> download some unix utilities that have been complied for windows
    >> e.g
    >> http://www.tailforwin32.sourceforge.net

    >
    > Or better yet, Cygwin.
    > <http://www.cygwin.com/>


    Or evn better, MKS.
    http://www.mkssoftware.com/products/tk/ds_tkdev.asp

    Though Cygwin has the advantage of being free.
     
    Mike Schilling, May 4, 2008
    #8
  9. (-Peter-) wrote:
    > Hi..
    >
    > I'm currently simulating some physics with a java program.
    >
    > The results is printed to a file line by line.
    >
    > The program takes several hours to run, so I would like to look at the
    > output as soon as it has been calculated.
    >
    > Can I do something so that the files closes after each cycle in the
    > program, so that I'm able to read it?
    >
    > /Peter


    Why don't you just display the output in a window as well as writing it
    to the file? If there is too much data to look at on the screen or you
    need to get another application on it, close the file at each stage and
    make a working copy, you can have the program notify you when they are
    ready. There is a lot of noise here for a really simple problem.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, May 4, 2008
    #9
  10. (-Peter-)

    Christian Guest

    (-Peter-) schrieb:
    > On 4 Maj, 02:09, Christian <> wrote:
    >> (-Peter-) schrieb:
    >>
    >>> Hi..
    >>> I'm currently simulating some physics with a java program.
    >>> The results is printed to a file line by line.
    >>> The program takes several hours to run, so I would like to look at the
    >>> output as soon as it has been calculated.
    >>> Can I do something so that the files closes after each cycle in the
    >>> program, so that I'm able to read it?
    >>> /Peter

    >> you can open a file and read it while it is being written to..
    >> What you probably want is calling flush()/force() on your
    >> stream/FileChannel so anything still cached is written out so you
    >> actually can see it with other programs.

    >
    > can you explain how to actually do this?
    >
    > /peter

    if you for example have something like

    BufferedOutputStream buf = new BufferedOutputstream(new
    FileOutputStream(file)));

    buf.write(foo);
    buf.flush(); //flush so eventually cached bytes are written to the
    underlying stream i.e. file
     
    Christian, May 4, 2008
    #10
  11. (-Peter-)

    BTDTGTTS Guest

    >
    > BufferedOutputStream buf = new BufferedOutputstream(new
    > FileOutputStream(file)));
    >
    > buf.write(foo);
    > buf.flush(); //flush so eventually cached bytes are written to the
    > underlying stream i.e. file


    OK - I'm going to bite & show my ignorance 'cos I haven't read the source,
    but surely a BufferedOutputStream must eventually write to to the uderlying
    stream when when the buffer is full. I know that then OS issues come into
    play, but even the OS has to write at some point.
     
    BTDTGTTS, May 4, 2008
    #11
  12. (-Peter-)

    Arne Vajhøj Guest

    Mike Schilling wrote:
    > Lew wrote:
    >> (-Peter-) wrote:
    >>>> The results is printed to a file line by line.
    >>>>
    >>>> The program takes several hours to run, so I would like to look at
    >>>> the output as soon as it has been calculated.

    >> BTDTGTTS wrote:
    >>> Can you not deal with this through your OS? Under linux try the
    >>> "tail" command. Don't know about Windows though, but you can
    >>> download some unix utilities that have been complied for windows
    >>> e.g
    >>> http://www.tailforwin32.sourceforge.net

    >> Or better yet, Cygwin.
    >> <http://www.cygwin.com/>

    >
    > Or evn better, MKS.
    > http://www.mkssoftware.com/products/tk/ds_tkdev.asp
    >
    > Though Cygwin has the advantage of being free.


    Cygwin also has more stuff.

    And all the cygwin stuff is the original stuff build
    from the same source as on Linux.

    Arne
     
    Arne Vajhøj, May 5, 2008
    #12
  13. Lew wrote:
    > Mike Schilling wrote:
    >> Or evn better, MKS.
    >>
    >> Though Cygwin has the advantage of being free.

    >
    > I never used an MKS product that I liked.


    I used to use the utilities (I think the package was called the MKS
    toolkit) and found them much more stable and less buggy than the
    Cygwin versions..
     
    Mike Schilling, May 5, 2008
    #13
  14. Arne Vajhøj wrote:
    > Mike Schilling wrote:
    >> Lew wrote:
    >>> (-Peter-) wrote:
    >>>>> The results is printed to a file line by line.
    >>>>>
    >>>>> The program takes several hours to run, so I would like to look
    >>>>> at
    >>>>> the output as soon as it has been calculated.
    >>> BTDTGTTS wrote:
    >>>> Can you not deal with this through your OS? Under linux try the
    >>>> "tail" command. Don't know about Windows though, but you can
    >>>> download some unix utilities that have been complied for windows
    >>>> e.g
    >>>> http://www.tailforwin32.sourceforge.net
    >>> Or better yet, Cygwin.
    >>> <http://www.cygwin.com/>

    >>
    >> Or evn better, MKS.
    >> http://www.mkssoftware.com/products/tk/ds_tkdev.asp
    >>
    >> Though Cygwin has the advantage of being free.

    >
    > Cygwin also has more stuff.
    >
    > And all the cygwin stuff is the original stuff build
    > from the same source as on Linux.


    Right, which means that Cygwin deals with Windows idiosyncracies (line
    termination, drive letters, extensions like .bat and .exe, etc.) less
    effectively than MKS does.

    > Arne
     
    Mike Schilling, May 5, 2008
    #14
  15. "Lew" <> wrote in message
    news:...
    > Mike Schilling wrote:
    >> Right, which means that Cygwin deals with Windows idiosyncracies (line
    >> termination, drive letters, extensions like .bat and .exe, etc.) less
    >> effectively than MKS does.

    >
    > I don't know how effectively MKS utilities deal with those things, so I
    > can't speak to the comparison, but the Cygwin utilities don't have
    > problems with those things.


    I recently installed Cygwin on a new PC, copied over my standard shell
    scripts, started it up, and nothing worked. Cygwin insisted that they
    contained illegal characters. What the .... Oh, right, Cygwin needs to be
    configured to understand CR/LF terminators.

    I have a number of scripts that find interesting files and pass their name
    as arguments to different utility programs. So long as I stay within the
    Cygwin world it all works great, but once I use non-Cygwin programs (gvim,
    javac, perforce, etc.) I have to remember to use the cygpath utility to
    convert /cygdrive/d//dir/foo.bar to d:\dir\foo.bar. That is, rather than
    being able to simply

    alias vi gvim

    I need the following:

    export SHELL=c:/cygwin/bin/tcsh
    args=""
    for arg in $*
    do
    arg=`cygpath -w $arg`
    args="$args $arg"
    done
    C:/Program\ Files/Vim/vim70/gvim.exe $args
     
    Mike Schilling, May 5, 2008
    #15
  16. (-Peter-)

    Christian Guest

    BTDTGTTS schrieb:
    >> BufferedOutputStream buf = new BufferedOutputstream(new
    >> FileOutputStream(file)));
    >>
    >> buf.write(foo);
    >> buf.flush(); //flush so eventually cached bytes are written to the
    >> underlying stream i.e. file

    >
    > OK - I'm going to bite & show my ignorance 'cos I haven't read the source,
    > but surely a BufferedOutputStream must eventually write to to the uderlying
    > stream when when the buffer is full. I know that then OS issues come into
    > play, but even the OS has to write at some point.
    >
    >

    yes eventually it must.
    Though if for example his prolgram only makes small outputs like one
    line of text.. or say 100 bytes per hour..
    then the stream might not write to disc for several hours.
    The buffered stream will only write when its buffer is filled up.
    Assume a buffersize of 512Byte that makes 6 hours.

    Christian


    Christian
     
    Christian, May 5, 2008
    #16
  17. (-Peter-)

    BTDTGTTS Guest

    Christian wrote:

    > BTDTGTTS schrieb:
    >>> BufferedOutputStream buf = new BufferedOutputstream(new
    >>> FileOutputStream(file)));
    >>>
    >>> buf.write(foo);
    >>> buf.flush(); //flush so eventually cached bytes are written to the
    >>> underlying stream i.e. file

    >>
    >> OK - I'm going to bite & show my ignorance 'cos I haven't read the
    >> source, but surely a BufferedOutputStream must eventually write to to the
    >> uderlying stream when when the buffer is full. I know that then OS issues
    >> come into play, but even the OS has to write at some point.
    >>
    >>

    > yes eventually it must.
    > Though if for example his prolgram only makes small outputs like one
    > line of text.. or say 100 bytes per hour..
    > then the stream might not write to disc for several hours.
    > The buffered stream will only write when its buffer is filled up.
    > Assume a buffersize of 512Byte that makes 6 hours.


    I take your point. As a follow up, is BufferedOutputStream.flush() a request
    or an instruction? Or is it OS dependant?
     
    BTDTGTTS, May 5, 2008
    #17
  18. BTDTGTTS wrote:
    > Christian wrote:
    >
    >> BTDTGTTS schrieb:
    >>>> BufferedOutputStream buf = new BufferedOutputstream(new
    >>>> FileOutputStream(file)));
    >>>>
    >>>> buf.write(foo);
    >>>> buf.flush(); //flush so eventually cached bytes are written to the
    >>>> underlying stream i.e. file
    >>> OK - I'm going to bite & show my ignorance 'cos I haven't read the
    >>> source, but surely a BufferedOutputStream must eventually write to to the
    >>> uderlying stream when when the buffer is full. I know that then OS issues
    >>> come into play, but even the OS has to write at some point.
    >>>
    >>>

    >> yes eventually it must.
    >> Though if for example his prolgram only makes small outputs like one
    >> line of text.. or say 100 bytes per hour..
    >> then the stream might not write to disc for several hours.
    >> The buffered stream will only write when its buffer is filled up.
    >> Assume a buffersize of 512Byte that makes 6 hours.

    >
    > I take your point. As a follow up, is BufferedOutputStream.flush() a request
    > or an instruction? Or is it OS dependant?
    >


    BufferedOuputStream.flush ensures that all data is flushed to the stream
    which it wraps and flush is called in turn. If the underlying stream is
    a FileOutputStream, then the data will reach the operating system and
    the OS flush method called (if applicable). However this does not imply
    that file metadata (length, modification time) is updated. Windows is an
    operating system where flushing the data is not sufficient to update the
    metadata. As a result applications which are 'watching' the file for any
    change may not see any change at the earliest possible time. To force
    the metadata to be updated without closing the file, use
    FileChannel.force(true).

    Mark Thornton
     
    Mark Thornton, May 5, 2008
    #18
  19. (-Peter-)

    Arne Vajhøj Guest

    Mike Schilling wrote:
    > Arne Vajhøj wrote:
    >> Mike Schilling wrote:
    >>> Lew wrote:
    >>>> (-Peter-) wrote:
    >>>>>> The results is printed to a file line by line.
    >>>>>>
    >>>>>> The program takes several hours to run, so I would like to look
    >>>>>> at
    >>>>>> the output as soon as it has been calculated.
    >>>> BTDTGTTS wrote:
    >>>>> Can you not deal with this through your OS? Under linux try the
    >>>>> "tail" command. Don't know about Windows though, but you can
    >>>>> download some unix utilities that have been complied for windows
    >>>>> e.g
    >>>>> http://www.tailforwin32.sourceforge.net
    >>>> Or better yet, Cygwin.
    >>>> <http://www.cygwin.com/>
    >>> Or evn better, MKS.
    >>> http://www.mkssoftware.com/products/tk/ds_tkdev.asp
    >>>
    >>> Though Cygwin has the advantage of being free.

    >> Cygwin also has more stuff.
    >>
    >> And all the cygwin stuff is the original stuff build
    >> from the same source as on Linux.

    >
    > Right, which means that Cygwin deals with Windows idiosyncracies (line
    > termination, drive letters, extensions like .bat and .exe, etc.) less
    > effectively than MKS does.


    Which must mean that Cygwin is better for nix emulation but MKS
    is better for win integration.

    Arne
     
    Arne Vajhøj, May 5, 2008
    #19
  20. Arne Vajhøj wrote:
    > Mike Schilling wrote:
    >> Arne Vajhøj wrote:
    >>> Mike Schilling wrote:
    >>>> Lew wrote:
    >>>>> (-Peter-) wrote:
    >>>>>>> The results is printed to a file line by line.
    >>>>>>>
    >>>>>>> The program takes several hours to run, so I would like to
    >>>>>>> look
    >>>>>>> at
    >>>>>>> the output as soon as it has been calculated.
    >>>>> BTDTGTTS wrote:
    >>>>>> Can you not deal with this through your OS? Under linux try the
    >>>>>> "tail" command. Don't know about Windows though, but you can
    >>>>>> download some unix utilities that have been complied for
    >>>>>> windows
    >>>>>> e.g
    >>>>>> http://www.tailforwin32.sourceforge.net
    >>>>> Or better yet, Cygwin.
    >>>>> <http://www.cygwin.com/>
    >>>> Or evn better, MKS.
    >>>> http://www.mkssoftware.com/products/tk/ds_tkdev.asp
    >>>>
    >>>> Though Cygwin has the advantage of being free.
    >>> Cygwin also has more stuff.
    >>>
    >>> And all the cygwin stuff is the original stuff build
    >>> from the same source as on Linux.

    >>
    >> Right, which means that Cygwin deals with Windows idiosyncracies
    >> (line termination, drive letters, extensions like .bat and .exe,
    >> etc.) less effectively than MKS does.

    >
    > Which must mean that Cygwin is better for nix emulation but MKS
    > is better for win integration.


    That's my impression, yes. Since what I want is a useful set of tools
    for Windows development, MKS is the superior toolset. Though MKS is
    pricey and Cygwin is, well, free.
     
    Mike Schilling, May 6, 2008
    #20
    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. Denon
    Replies:
    1
    Views:
    587
    Saravana [MVP]
    Nov 14, 2003
  2. Benjamin Rutt
    Replies:
    2
    Views:
    962
    Peter Hansen
    Dec 20, 2005
  3. Dani
    Replies:
    3
    Views:
    490
  4. Iñaki Baz Castillo
    Replies:
    7
    Views:
    893
    Iñaki Baz Castillo
    Jan 12, 2010
  5. Iulian Ilea
    Replies:
    1
    Views:
    326
    pcx99
    Dec 21, 2006
Loading...

Share This Page