将文件移动到特定文件夹

时间:2015-07-10 20:12:40

标签: java file-io

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

  public static void moveToRightDirectory(File song, String album) throws IOException {
    if(album.endsWith(" ")) { 
      album = album.substring(0, album.length() - 1);
    }
    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);
      }
    }
  }

我知道还有其他信息(从这些我受到启发),但我无法解决,所以我会请求你的帮助。

我想将song文件移到文件夹dir中。为此,我尝试了几种方法:

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

    线程“AWT-EventQueue-0”中的异常java.nio.file.InvalidPathException:尾随char< >在索引55:C:\ Users ... \ dir
        在sun.nio.fs.WindowsPathParser.normalize(未知来源)     在sun.nio.fs.WindowsPathParser.parse(未知来源)     在sun.nio.fs.WindowsPathParser.parse(未知来源)     在sun.nio.fs.WindowsPath.parse(未知来源)     at sun.nio.fs.WindowsFileSystem.getPath(Unknown Source)     在java.io.File.toPath(未知来源)     在createDir.CreateDirectory.moveToRightDirectory(CreateDirectory.java:73)     在createDir.CreateDirectory.createDirectory(CreateDirectory.java:40)     在gui.DirChooser.actionPerformed(DirChooser.java:54)     在javax.swing.AbstractButton.fireActionPerformed(未知来源)     at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)     在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)     在javax.swing.DefaultButtonModel.setPressed(未知来源)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源)     at java.awt.Component.processMouseEvent(Unknown Source)     在javax.swing.JComponent.processMouseEvent(未知来源)     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)     在java.awt.EventQueue.access $ 500(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     在java.awt.EventQueue $ 4.run(未知来源)     在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 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) - >它什么都不做。

  • FileUtils.moveFile(song, dir); - > Eclipse没有找到FileUtils。我执行了java.lang.Object.org.apache.commons.io.FileUtils的导入,但错误变为“导入java.lang.Object.org无法解析。”

我该如何解决? 非常感谢。

2 个答案:

答案 0 :(得分:2)

不是实际问题的答案,而是基于错误消息

  

java.nio.file.InvalidPathException:尾随字符< >在索引55:

我猜你的代码没有正确摆脱拖尾的空格。

尝试

album = album.trim();

而不是

if(album.endsWith(" ")) { 
  album = album.substring(0, album.length() - 1);
}

答案 1 :(得分:0)

http://www.mkyong.com/java/how-to-move-file-to-another-directory-in-java/

也许这会有所帮助

第1步:重命名文件

for(auto const& node : pt2.get_child("somelist"))
 write_json(std::cout, node.second);

}

第2步:复制并删除

import java.io.File;

public class MoveFileExample 
{
    public static void main(String[] args)
    {   
        try{

           File afile =new File("C:\\folderA\\Afile.txt");

           if(afile.renameTo(new File("C:\\folderB\\" + afile.getName()))){
            System.out.println("File is moved successful!");
           }else{
           System.out.println("File is failed to move!");
           }

         }catch(Exception e){
        e.printStackTrace();
    }
}