DevFaqModifyOpenFile

(Difference between revisions)
(Is it safe to programmatically modify a file which is open in the editor?)
 
(One intermediate revision not shown)
Line 1: Line 1:
-
__NOTOC__
 
__NOTOC__
__NOTOC__
===Is it safe to programmatically modify a file which is open in the editor?===
===Is it safe to programmatically modify a file which is open in the editor?===
-
It is not safe to modify a <tt>FileObject</tt> (via <tt>getOutputStream</tt> which is open and ''modified'' in the editor. In fact, it is not possible: calling <tt>FileObject.lock()</tt> will fail. If you modify the <tt>java.io.File</tt> (bypassing the Filesystems API) you may cause a conflict.
+
It is not safe to modify a <tt>FileObject</tt> (via <tt>getOutputStream</tt>) which is open and ''modified'' in the editor. In fact, it is not possible: calling <tt>FileObject.lock()</tt> will fail. If you modify the <tt>java.io.File</tt> (bypassing the Filesystems API) you may cause a conflict.
-
If the file is not modified in the editor (easily checkable via <tt>DataObject.isModified()</tt> then after your output stream is closed the file will be reloaded with the new contents.
+
If the file is not modified in the editor (easily checkable via <tt>DataObject.isModified()</tt>) 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 <tt>EditorCookie</tt> to acquire the Swing <tt>Document</tt> and make changes through that. It may be considered impolite to then save the file if it was modified before.
If the file ''is'' modified you can make changes to the open editor buffer: use <tt>EditorCookie</tt> to acquire the Swing <tt>Document</tt> and make changes through that. It may be considered impolite to then save the file if it was modified before.

Current revision as of 13:28, 16 June 2010

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

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

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo