Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6724329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:40:37+00:00 2026-05-26T09:40:37+00:00

The code below demonstarates the problem: java 7 interfaces for watching file changes report

  • 0

The code below demonstarates the problem: java 7 interfaces for watching file changes report opening of file, but don’t report actual change of content.

Is there a way to detect content change? My program needs to read new content as soon as is available.

The only length reported by event listener is 0 (right after opening of the file).

Compilable source to reproduce the problem:

import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.attribute.FileAttribute;

class NioModifiedProblem {
    public static void println(String str) {
        System.out.println(str);
    }
    public static void printFileInfo(Path path) {
        try {
            println(String.format("File %s, size %d, modified %s", path, Files.size(path), Files.getLastModifiedTime(path)));
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        String data = "Some not too long string goes here. Goes. Goes.";
        try {
            final Path path = Files.createTempFile("nioProblem", ".tmp", new FileAttribute[0]);
            path.toFile().deleteOnExit();
            println("Created");
            printFileInfo(path);

            Thread thread = new Thread() {
                public void run() {
                    try {
                        final Path parent = path.getParent();
                        final WatchService service = parent.getFileSystem().newWatchService();
                        WatchKey key = parent.register(service, StandardWatchEventKinds.ENTRY_MODIFY);
                        try {
                            while (true) {
                                for (WatchEvent<?> event : service.take().pollEvents()){
                                    Path modifiedPath = parent.resolve((Path)event.context());
                                    println("Path "+modifiedPath+" modified EVENT."); // This is printed only once, on file opening.
                                    printFileInfo(modifiedPath);
                                }
                            }
                        } catch (ClosedWatchServiceException e) {
                            println("Service closed");
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                    } finally {
                        println("Watcher thread exiting");
                    }
                }
            };
            thread.setDaemon(true);
            thread.start();

            Thread.sleep(1000);
            Writer fw = Files.newBufferedWriter(path, Charset.forName("UTF-8"));
            println("Opened");
            printFileInfo(path);

            Thread.sleep(1000);
            fw.write(data);
            println("Written");
            printFileInfo(path);

            fw.close();
            println("Closed");
            printFileInfo(path);

            Thread.sleep(1000);
            println("Sleeped");
            printFileInfo(path);
            return;
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

Output on Java(TM) SE Runtime Environment (build 1.7.0-b147):

Created
File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 0, modified 2011-09-14T16:20:06.782Z
Opened
Path C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp modified EVENT.
File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 0, modified 2011-09-14T16:20:07.807Z
File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 0, modified 2011-09-14T16:20:07.807Z
Written
File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 0, modified 2011-09-14T16:20:07.807Z
Closed
File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 47, modified 2011-09-14T16:20:08.81Z
Sleeped
File C:\Users\b\AppData\Local\Temp\nioProblem190636654560972941.tmp, size 47, modified 2011-09-14T16:20:08.81Z

The problem with the output – event is raised only once on file opening, and is never raised on actual writes or file closing.

Download source

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:40:37+00:00Added an answer on May 26, 2026 at 9:40 am

    WatchedKey should be reset after pollEvents() to be able to accept futher events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below works. But if I comment out the line Dim objRequest As
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
Code below is not working as expected to detect if it is in design
Code below is working well as long as I have class ClassSameAssembly in same
The code below shows a sample that I've used recently to explain the different
The code below gives me this mysterious error, and i cannot fathom it. I
The code below gives an error: Property 'Int32 Key' is not defined for type
The code below is the code i am using. It works fine in thunderbird
The code below continues many lines until it ends with a expected /veotherwise /vechoose.
The code below is checking performance of three different ways to do same solution.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.