Multiple streams per file

S

Shea Martin

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
 
A

Andrew Thompson

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?
 
C

Chris Head

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

Shea said:
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-----
 
R

Roedy Green

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.
 
M

Mike Schilling

Shea Martin said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top