无法写入自锁文件

Not able to write in self locked file

我遇到过两个进程正在使用文件的情况。所以我检查我的进程,如果文件已经被锁定,然后等到被解锁。

一旦它被另一个进程解锁,我就挂起我的锁然后开始读取和写入文件。但是我收到如下错误。我知道这是一个错误,因为它被我锁定了,但我想先锁定文件然后开始读写,以便其他进程在使用之前无法使用它。

Exception in thread "main" java.io.IOException: The process cannot access the file because another process has locked a portion of the file

这是我的代码片段

package RestClient;
import java.io.*;
import java.nio.Buffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;

public class Filelocking {

       public static void main(String[] args) throws IOException {

              String Name = "E:\RestAPI\Token.txt";
              File File = new File(Name);
              File FileRename = new File(Name);

              boolean FileEdit = false;

              if (!File.renameTo(FileRename)) {

                     while (!File.renameTo(FileRename)) {
                           System.out.println("rename failed");

                           if (File.renameTo(FileRename)) {
                                  FileEdit = true;
                                  break;
                           }

                     }

              }

              else {

                     FileEdit = true;

              }

              System.out.println("rename success");

              if (FileEdit) {

                  RandomAccessFile AccessFile = new RandomAccessFile(File, "rw");
                  FileChannel channel = AccessFile.getChannel();

                     FileLock lock = null;
                     try {

                           lock = channel.lock();
                           System.out.println("Lock Status: " + lock.isValid());
                           BufferedReader read = new BufferedReader (new FileReader(File));

                           System.out.println( read.readLine());




                     }

                     catch (OverlappingFileLockException e) {

                           System.out.println("File Lock Error: " + e.getMessage());
                     }

                     lock.close();

              }

       }

}

任何人都可以告诉我如何解决这个问题以及这里做错了什么吗?

是您自己的 FileLock 导致了问题。您应该从 FileLock 中读取文件。

您应该这样阅读:

ByteBuffer buffer = ByteBuffer.allocate(20);
int noOfBytesRead = channel.read(buffer);

您可以找到更多信息here

如果你想要阅读行,你可以这样做:

FileInputStream fin = new FileInputStream(file);
if (FileEdit) {

    RandomAccessFile accessFile = new RandomAccessFile(file, "rw");
    FileChannel channel = accessFile.getChannel();

        FileLock lock = null;
        try {

            lock = channel.lock();
            System.out.println("Lock Status: " + lock.isValid());
            InputStream is = Channels.newInputStream(channel);
            BufferedReader read = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            System.out.println(read.readLine());

        } catch (OverlappingFileLockException e) {
              System.out.println("File Lock Error: " + e.getMessage());
        }

        lock.close();

    }

如前所述here