Search code examples
javafilecopyexecutorservicemonitor

How to find very large file copying status in java


I have requirement for monitoring the files copying status in a directory and the files are placed continuously into the directory in java. I am planing to use Executor framework to find out individual files copy status and I have written below code but it is not working as expected, file without copy completion I am getting notification as copying got completed.

private boolean isFileCopied(String filePath) {
    File file = new File(filePath);

    Scanner scanner;
    boolean isCopied = true;
    while (true) {
        try {
            scanner = new Scanner(file);
            isCopied = false;
        } catch (FileNotFoundException e) {
            System.out.println(filePath + " File is in copy State. ");
            sleepFile();
        }
        if (isCopied == false) {
            break;
        }
    }
    System.out.println(filePath + "  copy completed");
    return isCopied;
}

   private static void sleepFile() {
    System.out.println("sleeping for 10 seconds");
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Please someone help me out how can I find the exact status of a file like file "copy in progress" or "copying done" and how can I monitor each and every file copying status If bunch of large files placed in a directory.

I have used watcher API but it is not solving my purpose. even the file without copying got completed I am getting notification as copying got completed. Below are my code changes.

Using watcher service:

import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;

public class FolderWatchDemo {
public static void main(String[] args) {

    final Path outputWatchFolderPath =  Paths.get("/outputFolder/");
    final Path sourceFolderPath = Paths.get("/sourceFolder/");

    try {
        //Registering outputWatchFolderPath
        WatchService watcher = FileSystems.getDefault().newWatchService();
        Path dir = Paths.get(outputWatchFolderPath.toString());

        dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
        System.out.println("Watch Service registered for dir: " + dir.getFileName());

        //copy files from inputfolder to o
        for (final Path path: Files.newDirectoryStream(sourceFolderPath))
            Files.copy(path, outputWatchFolderPath.resolve(path.getFileName()));

        while (true) {
            WatchKey key;
            try {
                key = watcher.take();
            } catch (InterruptedException ex) {
                return;
            }

            for (WatchEvent<?> event : key.pollEvents()) {
                 WatchEvent.Kind<?> kind = event.kind();

                WatchEvent<Path> ev = (WatchEvent<Path>) event;
                if (kind == ENTRY_CREATE) {
                    System.out.println("file got created !!");
                }
                if (kind == ENTRY_MODIFY) {
                    System.out.println("copying got completed !!");
                }
                if (kind == ENTRY_DELETE) {
                    System.out.println("file deleted successfully !!");
                }
            }
            boolean valid = key.reset();
            if (!valid) {
                break;
            }
        }

    } catch (IOException ex) {
        System.err.println(ex);
    }
}
}

Thanks in Advance.


Solution

  • There is a very elegant way to create a Call Back system and implementing it via implementing ReadableByteChannel in order to monitor the progerss of copying files. Also the benefit is there is no need to monitor a directory. You can explicitly monitor the progress of the file which is being copied.

    The main idea is proposed by this site, but changed it a little to fit in your problem:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.ByteBuffer;
    import java.nio.channels.Channels;
    import java.nio.channels.FileChannel;
    import java.nio.channels.ReadableByteChannel;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    interface ProgressCallBack {
        public void callback(CallbackByteChannel rbc, double progress);
    }   
    
    public class Main{
        public static void main(String[] args) {
    
            ProgressCallBack progressCallBack = new ProgressCallBack() {
                @Override
                public void callback(CallbackByteChannel rbc, double progress) {
                    System.out.println(rbc.getReadSoFar());
                    System.out.println(progress);
                }
            };
    
            try {
                copy("SOURCE FILE PATH", "DESTINATION FILE PATH", progressCallBack);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void copy(String source, String destination, ProgressCallBack callBack) throws IOException {
            FileOutputStream fos = null;
            FileChannel sourceChannel = null;
            try {
                sourceChannel = new FileInputStream(new File(source)).getChannel();
                ReadableByteChannel rbc = new CallbackByteChannel(sourceChannel, Files.size(Paths.get(source)), callBack);
                fos = new FileOutputStream(destination);
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(sourceChannel.isOpen()){
                    sourceChannel.close();
                }
                fos.close();
            }
        }
    }
    
    class CallbackByteChannel implements ReadableByteChannel {
        ProgressCallBack delegate;
        long size;
        ReadableByteChannel rbc;
        long sizeRead;
    
        CallbackByteChannel(ReadableByteChannel rbc, long expectedSize, ProgressCallBack delegate) {
            this.delegate = delegate;
            this.size = expectedSize;
            this.rbc = rbc;
        }
    
        public void close() throws IOException {
            rbc.close();
        }
    
        public long getReadSoFar() {
            return sizeRead;
        }
    
        public boolean isOpen() {
            return rbc.isOpen();
        }
    
        public int read(ByteBuffer bb) throws IOException {
            int n;
            double progress;
            if ((n = rbc.read(bb)) > 0) {
                sizeRead += n;
                progress = size > 0 ? (double) sizeRead / (double) size * 100.0 : -1.0;
                delegate.callback(this, progress);
            }
            return n;
        }
    }
    

    Hope this help.