如何使用Java检测指定Microsoft Word文档是否已关闭
Great question! Using Java's basic File API only tells you if a file exists—it can't directly detect if the document is currently open in Word or another app. The approach depends on your operating system, since file locking behaviors differ across platforms. Here are practical solutions:
Windows Platform
Windows uses exclusive file locks for open documents (like those opened in Word), so you can test if you can gain write access to the file. If you can't, it's likely locked by an active process (Word).
Try this utility method:
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class FileLockChecker { public static boolean isWordDocumentOpen(File file) { if (!file.exists()) { return false; // No file means it can't be open } // Try opening the file in append mode (won't overwrite content) try (FileOutputStream fos = new FileOutputStream(file, true)) { // Success: we can access the file, so it's not locked by Word return false; } catch (IOException e) { // IOException here usually means the file is exclusively locked (e.g., open in Word) return true; } } public static void main(String[] args) { File doc = new File("abc.docx"); boolean isOpen = isWordDocumentOpen(doc); System.out.println("Document is open: " + isOpen); } }
Note: This works for most standard Word scenarios, but some edge cases (like Word's auto-save temporary files) might behave differently. Always test with your specific workflow.
Linux/macOS Platform
Unix-like systems use advisory file locks, which aren't enforced—Word doesn't necessarily lock the file itself. Instead, you can check system processes to see if any are actively using the document. The lsof command lists open files and their associated processes.
Here's how to wrap that in Java:
import java.io.File; import java.io.IOException; public class UnixFileOpenChecker { public static boolean isWordDocumentOpen(File file) throws IOException, InterruptedException { String absolutePath = file.getAbsolutePath(); // Run lsof to check if the file is open by any process Process process = new ProcessBuilder("lsof", absolutePath).start(); int exitCode = process.waitFor(); // lsof returns 0 if it found open processes for the file, 1 if not boolean isOpen = exitCode == 0; // Optional: Filter to only Word processes for more accuracy // Process process = new ProcessBuilder("lsof", "-c", "Microsoft Word", absolutePath).start(); return isOpen; } public static void main(String[] args) throws IOException, InterruptedException { File doc = new File("abc.docx"); System.out.println("Document is open: " + isWordDocumentOpen(doc)); } }
Note: You'll need lsof installed on the system, and your Java process needs permission to run system commands. For better accuracy, filter by Word's process name (varies slightly between Linux and macOS).
General Caveats
- Race Conditions: The file state could change between your check and any subsequent action—always handle this in your logic.
- Network Files: These methods might not work reliably for documents on network shares, as locking behavior depends on the server setup.
- Direct Process Control: If your app is launching Word itself, consider using JNI (Windows) or AppleScript (macOS) to directly track the Word process's state for 100% accuracy.
内容的提问来源于stack exchange,提问作者sarah w




