Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to move a file to a specified folder but I can't.
This is the code:

Java
public static void moveToRightDirectory(File song, String album) throws IOException {
	    album = album.trim();
	    String pathDirectory = selectedDir + "\\" + album;
	    File dir = new File(pathDirectory);
	    System.out.println("dir.exists(): " + dir.exists());
	    if(dir.exists()) { 
	      Files.move(song.toPath(), dir.toPath(), StandardCopyOption.REPLACE_EXISTING);
	      //System.out.println(song.renameTo(dir));
	    }
	    else { 
	      boolean success = (new File(pathDirectory)).mkdirs();
	      if(!success) {
	        System.out.println("Error creating directory.");
	      }
	      else {
	        Files.move(song.toPath(), dir.toPath(), StandardCopyOption.REPLACE_EXISTING);
	        //System.out.println(song.renameTo(dir));
	        //FileUtils.moveFile(song, dir);
	      }
	    }
	  }


I would like to move the song file in the folder dir. To do this I have tried several methods:

- Files.move -> The following errors are generated:

Java
java.nio.file.FileSystemException: C:\Users\ila\Desktop\Adele1\Cold shoulder.mp3 -> C:\Users\ila\Desktop\Adele1\19: 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 createDir.CreateDirectory.moveToRightDirectory(CreateDirectory.java:74)
	at createDir.CreateDirectory.createDirectory(CreateDirectory.java:41)
	at gui.DirChooser.actionPerformed(DirChooser.java:54)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$500(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


- song.renameTo(dir) -> It does nothing.

- FileUtils.moveFile(song, dir) -> Eclipse does not find FileUtils. I did the import of java.lang.Object.org.apache.commons.io.FileUtils but the error becomes "The import java.lang.Object.org can not be resolved."

How can I fix?
Thanks a lot.
Posted

Did you read the error message?

It's telling you exactly what's wrong. Some process has the file open and you can't move, rename or open it until whatever process has the file locked closes it.

There's nothing you can do in your code to do that.
 
Share this answer
 
Please see my past answer: how to compress the error 'it is already used by another process' in vb.net[^].

You can ignore the parts specific to .NET; the idea is the same.

—SA
 
Share this answer
 
v2
Comments
Member 11829078 11-Jul-15 17:33pm    
Thanks for the reply. I read the message in this link but I didn't understand what to do. I downloading Handle.. Now what should I do?
Sergey Alexandrovich Kryukov 11-Jul-15 20:59pm    
Then use it. You should be able to know what process holds the file access.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900