繁体   English   中英

将文件移到特定文件夹

[英]Move a file to a specific folder

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

  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:在索引55处尾随char <>:C:\\ Users ... \\ dir
    在sun.nio.fs.WindowsPathParser.normalse(未知源)在sun.nio.fs.WindowsPathParser.parse(未知源)在sun.nio.fs.WindowsPathParser.parse(未知源)在sun.nio.fs.WindowsPath Sun.nio.fs.WindowsFileSystem.getPath的.parse(未知源),createDir.CreateDirectory.moveToRightDirectory(CreateDirectory.java:73)处java.io.File.toPath(未知源)处createDir.CreateDirectory处的.parse(未知源)。 gui的gui.DirChooser.actionPerformed(DirChooser.java:54)的createDirectory(CreateDirectory.java:40)在javax的javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)的javax.swing.AbstractButton.fireActionPerformed(Unknown Source)处。 javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知源)处的javax.swing.DefaultButtonModel.setPressed(未知源)处的java.awt.Component.processMouseEvent(未知源)处的swing.DefaultButtonModel.fireActionPerformed(未知源) javax.swing.JComponent.processMouseEvent(未知源),java.awt.Component.processEven t(未知源)在java.awt.Container.processEvent(未知源)在java.awt.Container.dispatchEventImpl(未知源)在java.awt.Component.dispatchEvent( java.awt.LightweightDispatcher.retargetMouseEvent的来源不明(java.awt.LightweightDispatcher.processMouseEvent的java.awt.LightweightDispatcher.dispatchEvent的来源不明(java.awt.Container.dispatchEventImpl的来源不明) )at java.awt.Window.dispatchEventImpl(未知源)at java.awt.Component.dispatchEvent(未知源)at java.awt.EventQueue.dispatchEventImpl(未知源)at java.awt.EventQueue.access $ 500(未知源)在java.awt.EventQueue $ 3.run(未知源)在java.awt.EventQueue $ 3.run(未知源)在java.security.AccessController.doPrivileged(本机方法)在java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知源) )at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 urce)at java.awt.EventQueue $ 4.run(未知源)at java.awt.EventQueue $ 4.run(未知源)at java.security.AccessController.doPrivileged(本机方法)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege( java.awt.EventQueue.dispatchEvent中的Unknown Source)(java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)中的java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)中的java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) )的java.awt.EventDispatchThread.pumpEvents(未知源)处的java.awt.EventDispatchThread.pumpEvents(未知的源)java.awt.EventDispatchThread.run的(未知源)

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

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

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

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

java.nio.file.InvalidPathException:在索引55处尾随char <>:

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

尝试

album = album.trim();

代替

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

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

也许这会有所帮助

步骤1:重命名文件

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();
    }
}

}

步骤2:复制和删除

   import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

        InputStream inStream = null;
    OutputStream outStream = null;

        try{

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

            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = inStream.read(buffer)) > 0){

                outStream.write(buffer, 0, length);

            }

            inStream.close();
            outStream.close();

            //delete the original file
            afile.delete();

            System.out.println("File is copied successful!");

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM