Unable to Verify Files That Should Be Java Files.exists

Asked 2 years ago, Updated 2 years ago, 39 views

java:jdk9.01
os:linux Red Hat 4.8.5-11

Check the Java code for files that other programs output every few seconds and
I'm trying to write a code that says I'll move on once I've confirmed it exists.

intn=0;
boolean flag = false;
while(flag==false){
    TimeUnit.SECONDS.sleep(waitsec);
    flag = Files.exists(path);
     n + = 1;
     if(n>limit){
         System.out.println("limit over");
     }
}

I'm writing like above, but
However, the flag is not true even though the file should have been printed.
Sometimes true comes back, but sometimes the timing is different.
If you check your existence by using the ls command, etc.,
Then the files.exists also get true.

I don't know why, but it's like a Schrodinger cat.
As a solution I found myself, I could manage it using Files.find, but
I want to know why Files.exists don't work well.

If anyone knows the cause, please let me know.

java linux

2022-09-30 14:20

2 Answers

If the file output is not flushed by the program on the file output side, the output may be buffered depending on the timing and not written to disk, so you may not be able to read it from other programs.

If it's C, there might be a change if you use fflush or fsync to explicitly flash the write.Also, with Java, if you call flush() in the implementation of Flushable interface such as PrintWriter, the write will be flushed.


2022-09-30 14:20

Monitoring Files Output from Other PCs on Network Drives

Based on this condition, I think it's because NFS space is used in asynchronous mode and files written on other PCs are not synchronized.

I think it will be resolved by specifying sync as the NFS server export option and sync as the NFS client mount option.

"Also, the operating system is ""RHEL4"", but according to Java9 operating conditions, it is RHEL6 or higher, so I think Java9 should be put off."

Also, the kernel of RHEL4 is 2.6.9, so I don't think the inotify-java API will work.(inotify-java site requires 2.6.13 or later)


2022-09-30 14:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.