Multiple streams per file

Discussion in 'Java' started by Shea Martin, Aug 16, 2005.

  1. Shea Martin

    Shea Martin Guest

    Will two File[Out|In]putStreams using the same File object cause havoc?
    i.e., I want two streams reading from the different locations in the
    file. Or is it better to have a seperate File object for each?

    ~S
    Shea Martin, Aug 16, 2005
    #1
    1. Advertising

  2. On Tue, 16 Aug 2005 17:53:13 GMT, Shea Martin wrote:

    > Will two File[Out|In]putStreams using the same File object cause havoc?


    What happened when you tried it?

    > i.e., I want two streams reading from the different locations in the
    > file.


    Why? Try to stop thinking in terms of *what* you
    want (to do) as opposed to what do you hope to
    achieve by that, what is the *point* of doing that?

    >..Or is it better to have a seperate File object for each?


    Or a RandomAccessFile, perhaps?

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "The shackles of commitment fell, in pieces on the ground" Led Zeppelin
    'Achilles Last Stand'
    Andrew Thompson, Aug 16, 2005
    #2
    1. Advertising

  3. Shea Martin

    Chris Head Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Shea Martin wrote:
    > Will two File[Out|In]putStreams using the same File object cause havoc?
    > i.e., I want two streams reading from the different locations in the
    > file. Or is it better to have a seperate File object for each?
    >
    > ~S


    Hi,
    I think READING should work fine, though writing will play havoc since
    if it even works at all your output will be interleaved. You might
    consider looking at RandomAccessFile.

    Chris
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (MingW32)

    iD8DBQFDAjYM6ZGQ8LKA8nwRAtpsAKDFZgUJUKpU98js4qG+WEb1s1K/AQCgn/Qy
    PtFmvUEJPNPoXQ2SfY6+oKA=
    =8hHN
    -----END PGP SIGNATURE-----
    Chris Head, Aug 16, 2005
    #3
  4. Shea Martin

    Roedy Green Guest

    On Tue, 16 Aug 2005 17:53:13 GMT, Shea Martin <null@void.0> wrote or
    quoted :

    >Will two File[Out|In]putStreams using the same File object cause havoc?
    > i.e., I want two streams reading from the different locations in the
    >file. Or is it better to have a seperate File object for each?


    It depends on whether you are trying to solve the problem for one
    platform or all. Java does not give you direct access to the OS file
    locking mechanisms.

    I would expect one of two behaviours:

    1. It is possible that on some platform, reading the file does a
    general lock on it.

    2. Java puts a prevent-write lock on a file whether reading or
    writing and prevent-reading lock on a file when writing.

    You can find out by experiment for single platform, but if you want to
    know the behaviour of all platforms you would have to dig in the
    language spec or talk nicely to Patricia Shanahan who has a gift for
    understanding the document.

    If the file is small, consider just reading it into RAM and sharing
    it.
    Roedy Green, Aug 16, 2005
    #4
  5. "Shea Martin" <null@void.0> wrote in message
    news:dKpMe.75643$...
    > Will two File[Out|In]putStreams using the same File object cause havoc?
    > i.e., I want two streams reading from the different locations in the file.
    > Or is it better to have a seperate File object for each?


    The File object is irrelevant here; it's just a way of naming the file.
    The question is whether you want two streams on the same actual file, and
    several of the posts in this topic already address that.

    Note that a FileOuputSream will either replace or append to an existing
    file. The only way you can open and modify an existing file is with a
    RandomAccessFile.
    Mike Schilling, Aug 17, 2005
    #5
    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. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,861
    gerry
    Apr 10, 2006
  2. Razvan
    Replies:
    1
    Views:
    402
    tony vee
    Sep 10, 2004
  3. Replies:
    5
    Views:
    2,519
  4. Replies:
    0
    Views:
    339
  5. Randy Kramer
    Replies:
    2
    Views:
    389
    Randy Kramer
    Jan 12, 2007
Loading...

Share This Page