Renaming a file throwing Exception in thread "main" java.nio.file.FileSystemException: -


I am looking into a directory to create new files or to create new files while coming into this directory. File Rename Required

I have the following code for it

  path pathfolder = Paths.get ("D: \\ tempm \\ watch"); Clock service provider = file system .get default (). NewWatchService (); Pathfolder.register (Viewer Service, Standard WatchEventCound .ENTRY_CREATE); System.out.println ("see that directory"); Boolean valid = true; Do {watch watch} = viewer service (); (WatchEvent & lt ;? & gt; for event: watchKey.pollEvents ()) {WatchEvent.Kind Type = 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); Old file name = new file ("D: \\ temp \\ clock \\" + filename); File destFile = new file ("D: \\ tempm \\ watch \\" + fileName.substring (0, filename .lastIndexOf (".")) + "_ Processing.jar"); File path = new file ("D: \\ tempm \\ clock \\"); If (! FileName.contains ("_ processing")) {Files.move (oldFileName.toPath (), destFile.toPath (), StandardCopyoption. Atomic_ MOVE, StandardCopyoption.REPLACE_EXISTING); // line number 45 fileDelaytestretti.fres.dat lete (old filename); System.out.println ("old file deleted"); }}}} Valid = watchKey.reset (); } While (valid);   

When I paste the file for the first time in that file, it has been successfully named. If I add a file the second time, it is throwing an exception. Why do not I have any ideas, this is an exception. Need some help.

My output

  View the file in that directory - dom4j.jar Old file deleted file - dom4j_processing.jar file created - thread "main" java.nio.file Watchplc.jar exception in FileSystemException: D: \ tempm \ watch \ watchplc.jar - & gt; D: \ tempm \ watch \ watchplc_processing.jar: Can not access the process 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 . (Unknown source) at java.nio.file.files.move (unknown source) at ext.gt.test.MonitorDirectory.main (unknown source) (Monitor directory .java45)    

Your program fails because StandardWatchEventKinds.ENTRY_CREATE only tells you that a file has been created and Not that any process has been stopped, write file or continue to handle the file. You can repeat the error by making a simple program yourself.

  Public class FileCreator {public static zero main (string ... args) {New FileCreator (). Go (); } Public going to zero () {FileWriter fileWriter = null; {File file = new file ("D: / templ / clock /" + system. Try content time emilis (+ + ".jar"); FileWriter = new file file (file); (Int counter = 0; counter & lt; 100; counter ++) {// first write filewriter.write (String.valueOf (System.nanoTime ())); }} Hold (IOException e) {e.printStackTrace (); } Finally {if (fileWriter! = Null) {try {// data is flushed and file handles stop fileWriter.flush (); FileWriter.close (); } Hold (IOException e) {e.printStackTrace (); }}}}   

Note that as soon as the first write () is encountered, a file is created for this file still in the program There is a handle and it continues to write until the stream flows and closes. Your second program picks up this file to move up before the stream is closed on it.

You need a system that tells you if the file is completely written in the directory you are viewing, to create a move on the file.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -