如何知道是否使用java进程关闭了文件

如何知道是否使用java进程关闭了文件,java,file,process,runtime.exec,Java,File,Process,Runtime.exec,我正在执行此命令,以便使用默认文件查看器打开日志文件: Process p = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler F:/Download/MSI3ca79.LOG"); 我想知道文件什么时候关闭。可能吗 p.waitFor(); // doesn't work because the process is terminated just after the execution. 解决方案: Pro

我正在执行此命令,以便使用默认文件查看器打开日志文件:

Process p = Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler F:/Download/MSI3ca79.LOG");
我想知道文件什么时候关闭。可能吗

p.waitFor(); // doesn't work because the process is terminated just after the execution.
解决方案:

ProcessBuilder pb = 
new ProcessBuilder("c:/windows/notepad.exe", "F:/Download/MSI3ca79.LOG");
File log = new File(LogFactory.getLogFactory(TestExternProcess.class).getName()); pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = pb.start();
int exitVal = p.waitFor();

显然,这是不可能的。我不会走这条路

它取决于特定的应用程序(记事本)打开文件。系统通常不知道应用程序何时停止查看文件。因为什么是关闭文件?是否正在关闭查看器UI中的选项卡?或者清理应用程序为文件分配的内存?关闭文档、xls文件时是否删除锁定文件


为了做到这一点,您需要编写一个程序来控制操作系统和任何可以查看文件的应用程序。

不幸的是,我这么认为。感谢您的回复。1)不要在问题中添加问候语或手势。2) 有关正确创建和处理流程的许多好提示,请参见。然后忽略它引用
exec
,并使用
ProcessBuilder
创建流程。还可以将
String arg
分解为
String[]args
,以说明包含空格字符的路径。。。3) “用默认的文件查看器打开日志文件”我会改为使用。这里的代码正在工作
ProcessBuilder pb=newprocessbuilder(“c:/windows/notepad.exe”,“F:/Download/MSI3ca79.log”);File log=新文件(LogFactory.getLogFactory(TestExternProcess.class).getName());pb.重定向错误流(真);pb.redirectOutput(Redirect.appendTo(log));进程p=pb.start();int exitVal=p.waitFor()很高兴你把它分类了。:)现在,您可以1)删除问题,或者(最好)2)输入下面的解决方案,并在站点允许时选择它作为答案。