1

In my app, i will check to see if unique id generated before, if not, it will generate one and write it to a file. But in a multiple processes app, it feels has problem when multiple process try to writing to this same file if they both find there's no a uid generated before.

So in android, how to prevent multiple process writing to the same file?

xiaoweiz
  • 162
  • 3
  • 10
  • It might be simplest to have only one process responsible for this - if the others see it needs to be done, they can use one of Android's many IPC mechanisms to request that that one do it. It could even be argued you should try to do all the access through one which will function as a server for the others. – Chris Stratton Apr 17 '15 at 03:58
  • @Chris thanks for the idea. But what if the process responsible for this is not running? – xiaoweiz Apr 17 '15 at 06:36
  • Mechanisms of communicating with it such as startService() would implicitly start it if not already running. – Chris Stratton Apr 17 '15 at 14:12
  • @Chris ok, i think use broadcast receiver here is proper. – xiaoweiz Apr 18 '15 at 05:34

1 Answers1

2

In android,you can use FileLock to lock a file to prevent another process from writing to that file.

A file lock can be : Exclusive or shared

Shared : Multiple processes can hold shared locks on the same region of a single file.

Exclusive : Only a single process can hold an exclusive lock. No other process can simultaneously hold a shared lock overlapping the exclusive.

final boolean isShared() : check wheather the file lock is shared or exclusive.

final long position() : lock's starting position in the file is returned.

abstract void release() : releases the lock on the file.

final long size() : returns length of the file that is locked.

Following example will clear your doubt how to lock a file and release it after performing operations on it.

 public void testMethod() throws IOException,NullPointerException{

    String fileName="textFile.txt";
    String fileBody="write this string to the file";
    File root;
    File textFile=null;
    //create one file inside /sdcard/directoryName/
    try
    {
        root = new File(Environment.getExternalStorageDirectory(),"directoryName");
        if (!root.exists()) {
            root.mkdirs();
        }
        textFile = new File(root, fileName);
        FileWriter writer = new FileWriter(textFile);
        writer.append(fileBody);
        writer.flush();
        writer.close();
        System.out.println("file is created and saved");
    }
    catch(IOException e)
    {
        e.printStackTrace();

    }
    //file created. Now take lock on the file
    RandomAccessFile rFile=new RandomAccessFile(textFile,"rw");
    FileChannel fc = rFile.getChannel();


    FileLock lock = fc.lock(10,20, false);
    System.out.println("got the lock");

    //wait for some time and release the lock
    try { Thread.sleep(4000); } catch (InterruptedException e) {}


    lock.release();
    System.out.println("released ");

    rFile.close();
}
N Kaushik
  • 2,198
  • 18
  • 29