Edit a file with an external application in Java and write back?
How do you edit a file from a Java application when you do not know which application the file is associated with. Also, because I'm using Java, I'd prefer a platform independent solution. After the external application was closed I want write the changes in the file back. The edit file was a local temp file. The real file is saved on a server, in an archive or any else.
I now I can use the class Desktop to open a file for edit. But how can I detect if the file 开发者_如何学编程was closed.
In this scenario where the external application is unknown and is not invoked from you java application then you can create a demon thread. I am assuming that you application knows the location of the file. This demon thread can check for any lock on the file and last updated time of the file when lock is released. If the last updated time changes you know file is changed.
Another option could be using 3rd party api like FileListener from apache which also does kind of the same thing internally.
I am not sure what you mean by 'After the external application was closed I want write the changes in the file back.'
If you just want to wait for the external application to finish, you can instead use RunTime :
Process p = Runtime.getRuntime.exec("mycommand");
p.waitFor()
精彩评论