Is it safe to programmatically modify a file which is open in the editor?

Apache NetBeans Wiki Index

Note: These pages are being reviewed.

It is not safe to modify a FileObject (via getOutputStream) which is open and modified in the editor. In fact, it is not possible: calling FileObject.lock() will fail. If you modify the java.io.File (bypassing the Filesystems API) you may cause a conflict.

If the file is not modified in the editor (easily checkable via DataObject.isModified()) then after your output stream is closed the file will be reloaded with the new contents.

If the file is modified you can make changes to the open editor buffer: use EditorCookie to acquire the Swing Document and make changes through that. It may be considered impolite to then save the file if it was modified before.

Applies to: NetBeans 4.0 and newer