0

Is it possible to access the same file and manipulate it from different working processes/thread at the same time?

If i open a file using the next code for example:

public static void main(String[] args){
    String dir = System.getProperty("user.dir");
    String path = dir + "/rfa.txt";

    try {
        RandomAccessFile file = new RandomAccessFile(path, "rw");
        file.seek(0);
        file.write("Hello world".getBytes());
        System.out.println(new String(bt));

        file.seek(30);
        file.write("Goodbye world".getBytes());

        file.close();
    } 
    catch (FileNotFoundException ex) {ex.printStackTrace();} 
    catch (IOException ex) {ex.printStackTrace();}
}

and i am running in a loop constantly. Can i open the same file from a different application and manipulate the data inside (read it or write new stuff), while the file still opened?

thank you for your help.

  • Which OS? Details vary and the details matter here. General answer is yes. But you are responsible for synchronisation (and remember IO operations are not atomic); and you may need to explicitly allow multiple writers. – Richard Dec 21 '16 at 08:43
  • currently on windows but in the future it will be on linux (centos). so how do i explicitly allow that? –  Dec 21 '16 at 08:58
  • No idea in what appears to be Java: check for sharing options when opening file. – Richard Dec 21 '16 at 09:32
  • You can try sync with semaphores you access to the file or try this http://stackoverflow.com/questions/9093888/fastest-way-of-reading-relatively-huge-byte-files-in-java – Raskayu Dec 21 '16 at 12:23

0 Answers0