blob: c487292251c2937d490b88438c31f1e51aec8d74 [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqListeningForFileChanges
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqListeningForFileChanges
:description: Apache NetBeans wiki DevFaqListeningForFileChanges
:toc: left
:toc-title:
:syntax: true
=== I am listening for changes in a folder/file but when there are changes I do not receive an event
If you are adding/deleting/changes using the FileSystems API - i.e. adding using link:http://bits.netbeans.org/dev/javadoc/org-openide-filesystems/org/openide/filesystems/FileUtil.html#createData(org.openide.filesystems.FileObject,%20java.lang.String)[FileUtil.createData()] then it should work.
If not, get the parent folder as a FileObject and call theFolder.getFileSystem().refresh().
NetBeans will automatically do the refresh if focus is shifted away from the main window and back. However if files are created by an external process, or by using java.io.File.mkdir() or java.io.File.createNewFile() (which you shouldn't be using inside a NetBeans module unless you really need to), changes will not be noticed until something forces a refresh.
The reasons for the way this is handled are twofold:
1. There is not native support in Java for listening for changes in a file/folder (yet), though most OS's natively support it
2. We used to use a system that polled on a timer, but this is a bad idea because the user can be using, say, email, and most of the in-memory copy of NetBeans can have been swapped out to disk by the OS - but to check for modified files, it has to be dragged back into memory out of the swap file to see if some files have changed, even though the user is using another application. That hurts performance of the user's other applications.
If you are creating files or folders programmatically, use link:http://bits.netbeans.org/dev/javadoc/index.html[FileObject], not java.io.File and any nodes your UI shows for those files will automatically update.
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqListeningForFileChanges[http://wiki.netbeans.org/DevFaqListeningForFileChanges] ,
that was last modified by NetBeans user Tboudreau
on 2010-01-23T20:41:36Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.