Basic I/O Question

R

Rhino

What happens to a file if a Java application opens it, writes to it, but
then ends without closing the file?

I've got a file that is having lines appended to it via one of my programs.
However, some of the things that I should be seeing in the file aren't
there. I've also found that I can't delete the file because Windows says
that it is "in use", even though no other application should be touching it
and my program has ended. In certain cases, I suspect that the file is not
being closed by my program so I wonder if that could explain the behaviour
I'm seeing.

Can anyone enlighten me on this?

--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
 
K

kaeli

rhino1 said:
In certain cases, I suspect that the file is not
being closed by my program so I wonder if that could explain the behaviour
I'm seeing.

Can anyone enlighten me on this?

I always make it a habit to flush the buffer and close any file (or stream) I
write to.
If it is at all possible for an application to throw an exception during the
time the file is open, the flush and close are in a finally block (unless, of
course, the exception is that the file couldn't be found or written to).

I don't know if it's completely necessary, but I haven't had problems yet.

--
 
R

Rhino

kaeli said:
I always make it a habit to flush the buffer and close any file (or stream) I
write to.
If it is at all possible for an application to throw an exception during the
time the file is open, the flush and close are in a finally block (unless, of
course, the exception is that the file couldn't be found or written to).

I don't know if it's completely necessary, but I haven't had problems yet.
I agree that those are good practices but do you know what happens to a file
that is inadvertently left open when the program ends?

Rhino
 
P

Phil Staite

I agree that those are good practices but do you know what happens to a file
that is inadvertently left open when the program ends?

I suspect that it may be environment (OS/JVM) dependent. If the java
program has written to the file, then some data may be in a buffer (ie.
memory owned by the JVM) and some may have been written to the file.
(ie. in the OS's buffers or actually out on the disk). If the process
(from the OS's viewpoint) terminates with open file handles... I
suspect most OSs will simply close the file in the state it is in. ie.
possibly with some data in it, some (or all?) held in the JVM's buffers.
I believe you could count on most OSs to at least flush their buffers
before closing any files open by an exiting process...
 
G

Gordon Beaton

I agree that those are good practices but do you know what happens
to a file that is inadvertently left open when the program ends?

Virtually all operating systems will close the file properly.

However when the application writes to the file using library
routines, those routines may buffer some of the data before actually
writing it to the file. If there is unwritten buffered data when the
process terminates, it will be lost since the operating system has no
way of knowing about it. It will appear as though the file has been
truncated near the end.

/gordon
 
R

Rhino

Gordon Beaton said:
Virtually all operating systems will close the file properly.

However when the application writes to the file using library
routines, those routines may buffer some of the data before actually
writing it to the file. If there is unwritten buffered data when the
process terminates, it will be lost since the operating system has no
way of knowing about it. It will appear as though the file has been
truncated near the end.

/gordon
Thanks to everyone who replied to my question.

Rhino
 
A

Alan Krueger

Rhino said:
However when the application writes to the file using library
routines, those routines may buffer some of the data before actually
writing it to the file. If there is unwritten buffered data when the
process terminates, it will be lost since the operating system has no
way of knowing about it. It will appear as though the file has been
truncated near the end.
[...]

Thanks to everyone who replied to my question.

You may wish to note that the Javadac for the FileOutputStream.finalize
method states that FileOutputStream.close will be called as the stream
is garbage collected. While finalization may or may not occur at JVM
shutdown (see Runtime.exit), it's possible for the object to get
garbage-collected between the time the stream was last used and JVM
shutdown.

Because of all this uncertainty, it's obviously better to close it when
you're done with it.
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top