4

I want to check that file is opened by another process. How can I implement such functionality?

In Windows I can write something like

try {
    openFileWriter(file);
    // File not locked => not opened by another process
} catch (IOException) { 
    // File is locked => opened by another process
}

In Linux I can use lsof.

But is there any common solution for both Linux and Windows?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Nikita Koval
  • 107
  • 5
  • Why your code wouldn't throw `IOException` in Linux? – dguay Oct 19 '15 at 17:11
  • Because condition `File not locked => not opened by another process` isn't true. You can edit file by multiple processes in Linux. – Nikita Koval Oct 19 '15 at 17:24
  • Thanks I didn't know that. I can't help then good luck. – dguay Oct 19 '15 at 17:26
  • @FurkanYavuz Not a duplicate, that question is about checking if a file is locked, and the answers on that question focus on Windows. This question is about checking if a file is **opened** (which doesn't necessarily mean it's locked). – Mark Rotteveel Oct 27 '18 at 10:46
  • the answer looks like "no, not at the moment" (you will need platform dependent/checking code to implement this functionality (platform independent))....but it needs longer text to proof :) – xerx593 Nov 21 '18 at 08:46

0 Answers0