How to test if a file is “complete” (completely written) with Java

后端 未结 10 1625
再見小時候
再見小時候 2020-12-05 04:21

Let\'s say you had an external process writing files to some directory, and you had a separate process periodically trying to read files from this directory. The problem to

10条回答
  •  心在旅途
    2020-12-05 05:05

    This possible to do by using Apache Commons IO maven library FileUtils.copyFile() method. If you try to copy file and get IOException its means that file is not completely saved.

    Example:

    public static void copyAndDeleteFile(File file, String destinationFile) {
    
        try {
            FileUtils.copyFile(file, new File(fileDirectory));
        } catch (IOException e) {
            e.printStackTrace();
            copyAndDeleteFile(file, fileDirectory, delayThreadPeriod);
        }
    

    Or periodically check with some delay size of folder that contains this file:

    FileUtils.sizeOfDirectory(folder);
    

提交回复
热议问题