OK, I guess(guess!) I understand you now. First, it depends on your OS. Sometimes your OS will do this work(exclusively operate a file) for you. Second, you may use the key word "synchronized" to adjust statement(s) to let this statement(s) can be invoked by only one thread. So, a file can only be accessed by one user.
May be I will send you short sample codes.