It is often the case that several programs process the same file, such as updating or reading the file at the same time. Such issues should be dealt with, otherwise chaos may ensue. After JDK 1.4, Java provides a file lock function that can help solve such problems.
The FileLock and FileChannel classes are in the java.nio and java.nio.channels packages respectively. File locks can be used when the input and output streams read and write files. The following uses the RandomAccessFile class to illustrate the use of file locks.
The stream created by RandomAccessFile can use file locks when reading and writing files. As long as the lock is not released, other programs cannot operate the locked file.
The steps to use file lock are as follows:
1) First use the RandomAccessFile stream to create a stream object pointing to the file. The read-write attribute of the object must be rw, for example:
RandomAccessFileinput=newRandomAccessFile(Main.java,rw);
2) The input stream calls the method getChannel() to obtain a FileChannel object (channel) connected to the underlying file, for example:
FileChannelchannel=input.getChannel();
3) The channel calls the tryLock() or lock() method to obtain a FileLock (file lock) object. This process is also called locking the file, for example:
FileLocklock=channel.tryLock();
After the file lock object is generated, any program will be prohibited from operating or locking the file. After locking a file, if you want to read or write the file, you must let the FileLock object call release() to release the file lock, for example:
lock.release();
For example, a Java program releases a file lock with each click of a button, reads a line of text from the file, and then locks it immediately. When a Java program is running, users cannot use other programs to operate files locked by the current Java program. For example, users cannot save files locked by the current Java program using the "Notepad" program (Notepad.exe) provided by the Windows operating system. .
Main.java
importjava.io.*;publicclassMain{publicstaticvoidmain(Stringargs[]){Filefile=newFile(Main.java);WindowFileLockwin=newWindowFileLock(file);win.setTitle(use file lock);}}
WindowFileLock.java
importjava.io.*;importjava.nio.*;importjava.nio.channels.*;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassWindowFileLockextendsJframeimplementsActionListener{JTextAreatext;JButtonbutton;Filefile;RandomAccessFileinput; FileChannelchannel;FileLocklock;WindowFileLock(Filef){file=f;try{input=newRandomAccessFile(file,rw);channel=input.getChannel();lock=channel.tryLock();}catch(Exceptionexp){}text=newJTextArea ();button=newJButton(read a line);button.addActionListener(this);add(newJScrollPane(text),BorderLayout.CENTER);add(button,BorderLayout.SOUTH);setSize(300,400);setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}publicvoidactionPerformed(ActionEvente){try{lock.release();StringlineString=input.readLine();text.append(n+lineString);lock=channel.tryLock();if(lineString ==null)input.close();}catch(Exceptionee){}}}