簡體   English   中英

重命名線程“ main” java.nio.file.FileSystemException中的引發異常的文件:

[英]Renaming a file throwing Exception in thread “main” java.nio.file.FileSystemException:

我正在尋找使用WatchService創建新文件的目錄。 每當創建新文件或進入該目錄時,我都需要更改該文件的名稱。

我有以下代碼

Path pathfolder=Paths.get("D:\\tempm\\watch");
        WatchService watcherService=FileSystems.getDefault().newWatchService();
        pathfolder.register(watcherService, StandardWatchEventKinds.ENTRY_CREATE);

        System.out.println("Watching that directory");
        boolean valid=true;
        do{
            WatchKey watchKey=watcherService.take();
            for(WatchEvent<?> event:watchKey.pollEvents()){
                WatchEvent.Kind kind=event.kind();
                if(StandardWatchEventKinds.ENTRY_CREATE.equals(event.kind())){
                    final String fileName=event.context().toString();
                    if(fileName.endsWith(".jar")){
                    System.out.println("File created--"+fileName);
                    File oldFileName=new File("D:\\tempm\\watch\\"+fileName);

                        File destFile=new File("D:\\tempm\\watch\\"+fileName.substring(0,fileName.lastIndexOf("."))+"_processing.jar");
                        File path=new File("D:\\tempm\\watch\\");
                            if(!fileName.contains("_processing")){
                        Files.move(oldFileName.toPath(), destFile.toPath(),StandardCopyOption.ATOMIC_MOVE,StandardCopyOption.REPLACE_EXISTING); //Line Number 45
                        FileDeleteStrategy.FORCE.delete(oldFileName);
                        System.out.println("Old file deleted");
                            }
                        }
                    }
            }
            valid=watchKey.reset();
        }while(valid);

當我第一次將文件粘貼到該目錄中時,它已成功重命名;如果我第二次添加任何文件,它將引發異常。我不知道為什么會給出該異常。需要一些幫助。

我的輸出

Watching that directory
File created--dom4j.jar
Old file deleted
File created--dom4j_processing.jar
File created--watchplc.jar
Exception in thread "main" java.nio.file.FileSystemException: D:\tempm\watch\watchplc.jar -> D:\tempm\watch\watchplc_processing.jar: The process cannot access the file because it is being used by another process.

    at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsFileCopy.move(Unknown Source)
    at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
    at java.nio.file.Files.move(Unknown Source)
at ext.gt.test.MonitorDirectory.main(MonitorDirectory.java:45)

您的程序失敗,因為StandardWatchEventKinds.ENTRY_CREATE僅告訴您已創建文件,而不是告訴進程已停止寫入文件或釋放文件句柄。 您可以通過自己創建一個簡單的程序來復制錯誤。

public class FileCreator
{
    public static void main(String... args)
    {
        new FileCreator().go();
    }

    public void go()
    {
        FileWriter fileWriter = null;
        try
        {
            File file = new File("d:/tempm/watch/" + System.currentTimeMillis() + ".jar");
            fileWriter = new FileWriter(file);
            for (int counter = 0; counter < 100; counter++)
            {
                // first write
                fileWriter.write(String.valueOf(System.nanoTime()));
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(fileWriter != null)
            {
                try
                {
                    // data is flushed and file handles are closed
                    fileWriter.flush();
                    fileWriter.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

請注意,一旦遇到第一個write() ,就會立即創建一個文件。 該程序仍然具有文件的句柄,並繼續對其進行寫入,直到刷新並關閉流為止。 您的其他程序在流關閉該文件之前,先將其移走。

在嘗試move文件之前,您需要一個系統來告訴您文件是否已完全寫入要查看的目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM