0

i already tried with this

if (!readAttributes.creationTime().equals(readAttributes.lastModifiedTime()))

but in some cases this gave me false while it was ended i am searching for a less cost effective way.

i know there are ways that are costly.

without buffer

files.copy

public static boolean isFileInUse(Path path) {
        try {
            LOGGER.info(path.toString());
            BasicFileAttributes readAttributes = Files.readAttributes(path, BasicFileAttributes.class);
            LOGGER.info("File TIME-->>   " + readAttributes.creationTime() + " :: " + readAttributes.lastModifiedTime());
            if (!readAttributes.creationTime().equals(readAttributes.lastModifiedTime()))
                return false;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
D. Lawrence
  • 943
  • 1
  • 10
  • 23
  • 2
    If the underlying system has mandatory file locks, just open and lock the file. If not, there is no way to check that. Nothing you can check in this method prevents another process from opening the file for writing right when this method returns. – Holger Feb 12 '20 at 10:12
  • 2
    *how to check if a file is being written so that i can wait for copying it* The first thing to do is stop trying to treat a filesystem as a message queue of some sort - it's not one. If you want something **reliable** you have to design a system where the sending process sends a signal of some sort that it's done writing the file. **You can not assume that the file no longer being open or actively written to that the sending process is actually done and that the file is complete.** The sender can rename the file when it's complete, for example. But the sender has to do it. – Andrew Henle Feb 14 '20 at 13:42
  • 1
    @Holger Even locking schemes can't tell you if the sender successfully wrote the entire file. Things like "the file is successfully transferred" are events. File systems are state-based and have no real concept of events. Trying to force event frameworks onto filesystems is [horribly unreliable](https://stackoverflow.com/questions/15806488/inotify-missing-events). – Andrew Henle Feb 14 '20 at 13:45
  • 1
    @AndrewHenle of course, you are right. The best thing it can ensure, is that no-one is going to write more data while you are reading it. In the usual case, not even the already mentioned prerequisite (“the underlying system’s file locks are mandatory”) will hold. – Holger Feb 14 '20 at 13:54
  • @AndrewHenle apart from renaming the file name is it a possible way ? – Jaskaran Singh Feb 15 '20 at 12:30
  • 1
    @JaskaranSingh If you want to be completely reliable, you need some way for the **sender** to atomically signal that the file has been fully and successfully written. A rename from, for exampe, `filename.writing` to just `filename` is one way. Such renames work because they're an atomic operation in most current filesystems. Another would be for the sender to create an empty `filename.done` file when `filename` is complete. The sender can move a file from an `inwork` to a `done` directory - if both directories are in the same filesystem, such moves are also atomic. – Andrew Henle Feb 15 '20 at 16:22
  • 1
    (cont) The problem is, without such a signal from the sender, you have know way to **KNOW** that the file is completely and successfully written as you have no way to obtain that information. Some files (such as PDFs) have information in them that allow you to know that they are complete, but that means you have to read the entire file to check. Most people who use file transfers as messaging don't think about this. But if you want to be truly reliable, you have to. File copy/write operations can fail - network connections can be lost, writing processes can crash. – Andrew Henle Feb 15 '20 at 16:25
  • 1
    (cont) Common methods in use - such as "the file is no longer open by the writing process" - can not tell you **WHY** the file is no longer open by that process. Did the process crash? Did it encounter an error condition? Did some construction equipment just cut your network connection? You can't tell. If processing a partial or errored-out file isn't a problem worth solving, then you don't really have to spend much time on this. But if processing such a file can cause real problems, you really should. – Andrew Henle Feb 15 '20 at 16:28
  • @AndrewHenle thanks for that but my application works on the host, and the above method that i used worked perfectly but late i found out the host is not closing the file properly i.e. making the create date and modified date same. – Jaskaran Singh Feb 16 '20 at 09:49
  • (cont) @AndrewHenle i cannot make any amendments in the host my application needs to work as a parasite. – Jaskaran Singh Feb 16 '20 at 09:49

0 Answers0