将文件移动到特定文件夹 [英] Move a file to a specific folder

查看:90
本文介绍了将文件移动到特定文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件移动到指定的文件夹,但我不能。

这是代码:



  public   static   void  moveToRightDirectory(文件歌曲,字符串专辑)抛出 IOException {
album = album。修剪();
字符串 pathDirectory = selectedDir + \ \ +专辑;
File dir = new 文件(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 文件(pathDirectory))。mkdirs();
if (!success){
System.out.println( < span class =code-string>创建目录时出错。);
}
else {
Files.move(song.toPath(),dir.toPath(),StandardCopyOption.REPLACE_EXISTING);
// System.out.println(song.renameTo(dir));
// FileUtils.moveFile(song,dir);
}
}
}





我想将歌曲文件移到文件夹中DIR 的。为此,我尝试了几种方法:



- Files.move - >生成以下错误:



 java.nio.file.FileSystemException:C:\ Users \ pi \Desktop \\ \\Adele1 \Cold shoulder.mp3  - > C:\Users\ila\Desktop\Adele1 \19:进程无法访问该文件,因为它正由另一个进程使用。 

at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs .WindowsFileCopy.move(未知来源)
at sun.nio.fs.WindowsFileSystemProvider.move(未知来源)
at java.nio.file.Files.move(未知来源)
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(未知来源)
at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(未知来源)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源)
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(未知来源)
at java.awt.EventQueue $ 3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown)来源)
at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue $ 4.run(Unknown Source)
at java.awt.EventQueue $ 4.run(未知来源)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown)来源)
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.ru n(未知来源)





- song.renameTo(dir) - >它什么都不做。



- FileUtils.moveFile(song,dir) - > Eclipse找不到 FileUtils 。我做了 java.lang.Object.org.apache.commons.io.FileUtils 的导入,但错误变成导入 java.lang.Object.org 不能得到解决。



如何解决?

非常感谢。

解决方案

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(未知来源)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatch er.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(未知来源)
at java.awt.EventQueue.access


500(未知来源)
at java.awt.EventQueue


< blockquote> 3.run(未知来源)
at java.awt.EventQueue


I'm trying to move a file to a specified folder but I can't.
This is the code:

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.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.

解决方案

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


这篇关于将文件移动到特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆