You can use one instance in you program for all threads that write to this file. In this case the file must be open all the time the program is running:
on startup - open the file
FileOutputStream fos = new FileOutputStream("myFile")
every thread must enclose the usage of this output stream when it writes data to it:
synchronized (fos) {
// write data
}
note that all the threads must use the same 'fos' object. You can do the same with RandomAccessFile - the important think is that the synchronization must be performed on *one* objects.
Main Topics
Browse All Topics





by: nir2002Posted on 2002-08-13 at 00:15:20ID: 7216360
Hi,
If you open specfic file for write the os will lock the file for you as long as the file is open.
another way is to change the permission to read only, copy the file and do the changes later do the changes in the original(assuming program the see the file read-only status don't try to change permission and than modify)
Hope it help
Best regards
Nir