hi,
I have a probelm,two thread from two different JVM's accessing the same file causing improper data for the 2 JVM's resulting in improper data.
If it same JVM i can go for synchronized block ,wil not happen with independent JVM.soluion pls?

Recommended Answers

All 3 Replies

You will need to use file locking. There are methods for controlling this in FileChannel.

hi thanks for the reply can someone explain me more about the API for file locking

hi thanks for the reply can someone explain me more about the API for file locking

From FileChannel javadoc:

a file channel can be obtained from an existing FileInputStream, FileOutputStream, or RandomAccessFile object by invoking that object's getChannel method, which returns a file channel that is connected to the same underlying file.

The FileChannel has methods to obtain a FileLock object on all or part of a file channel. When no longer needed, FileLock.release() will release the lock.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.