如何在java中找到非常大的文件复制状态



我需要监控目录中的文件复制状态,并且这些文件在java中被连续放置到目录中。我计划使用Executor框架来了解单个文件的复制状态,我已经写了下面的代码,但它没有按预期工作,文件没有复制完成,我收到了复制完成的通知。

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();
    }
}

请有人帮我找出如何找到文件(如文件"正在复制"或"复制完成")的确切状态,以及如何监控每个文件的复制状态如果一个目录中有一堆大文件。

我使用了观察者API,但它并没有解决我的目的。即使没有复制的文件也完成了我收到了复制完成的通知。以下是我的代码更改。

正在使用观察程序服务:

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);
    }
}
}

提前感谢。

有一种非常优雅的方法可以创建Call Back系统,并通过实现ReadableByteChannel来实现它,以监控复制文件的进程。另外,好处是不需要监视目录。您可以显式监视正在复制的文件的进度。

主要的想法是由这个网站提出的,但为了适应你的问题,它做了一些改变:

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;
    }
}

希望得到帮助。

package test;
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.StandardCopyOption;
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()),
                                    StandardCopyOption.REPLACE_EXISTING);
            String fileName = null;
            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 !!" + ev.context());
                    }
                    if (kind == ENTRY_MODIFY) {
                        String fName = ev.context().getFileName().toString();
                        if (fileName != null && !fName.equals(fileName)) {
                            System.out.println("file copying completed !!" + fileName);
                        }
                        fileName = fName;
                        System.out.println("copying " + fName);
                    }
                    if (kind == ENTRY_DELETE) {
                        System.out.println("file deleted successfully !!");
                    }
                }
                boolean valid = key.reset();
                if (!valid) {
                    break;
                }
            }
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新