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?

    Shea Martin, Aug 16, 2005
    1. Advertisements

  2. What happened when you tried it?
    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 a RandomAccessFile, perhaps?
    Andrew Thompson, Aug 16, 2005
    1. Advertisements

  3. Shea Martin

    Chris Head Guest

    Hash: SHA1

    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.

    Version: GnuPG v1.2.1 (MingW32)

    -----END PGP SIGNATURE-----
    Chris Head, Aug 16, 2005
  4. Shea Martin

    Roedy Green Guest

    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
    Roedy Green, Aug 16, 2005
  5. 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
    Mike Schilling, Aug 17, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.