如何将文件从一个位置移动到另一位置?当我运行程序时,在该位置创建的任何文件都会自动移动到指定位置。我怎么知道哪个文件被移动了?
Answers:
myFile.renameTo(new File("/the/new/place/newName.file"));
File#renameTo可以做到这一点(它不仅可以重命名,而且可以在目录之间移动,至少在同一文件系统上)。
重命名此抽象路径名表示的文件。
此方法的行为的许多方面本质上都依赖于平台:重命名操作可能无法将文件从一个文件系统移动到另一个文件系统,它可能不是原子的,并且如果目标抽象路径名的文件可能无法成功已经存在。应该始终检查返回值,以确保重命名操作成功。
如果您需要更全面的解决方案(例如想要在磁盘之间移动文件),请查看Apache Commons FileUtils#moveFile
myFile此命令不会更新对象的路径。因此它将指向不再存在的文件。
                    使用Java 7或更高版本,您可以使用Files.move(from, to, CopyOption... options)。
例如
Files.move(Paths.get("/foo.txt"), Paths.get("bar.txt"), StandardCopyOption.REPLACE_EXISTING);
有关更多详细信息,请参见文件文档。
要移动文件,您还可以使用Jakarta Commons IOs FileUtils.moveFile
如果出错,它将抛出一个IOException,因此当没有异常抛出时,您就知道该文件已移动。
File.renameToJava IO中的Java可以用来在Java中移动文件。另请参阅此SO问题。
只需添加源和目标文件夹路径。
它将所有文件和文件夹从源文件夹移动到目标文件夹。
    File destinationFolder = new File("");
    File sourceFolder = new File("");
    if (!destinationFolder.exists())
    {
        destinationFolder.mkdirs();
    }
    // Check weather source exists and it is folder.
    if (sourceFolder.exists() && sourceFolder.isDirectory())
    {
        // Get list of the files and iterate over them
        File[] listOfFiles = sourceFolder.listFiles();
        if (listOfFiles != null)
        {
            for (File child : listOfFiles )
            {
                // Move files to destination folder
                child.renameTo(new File(destinationFolder + "\\" + child.getName()));
            }
            // Add if you want to delete the source folder 
            sourceFolder.delete();
        }
    }
    else
    {
        System.out.println(sourceFolder + "  Folder does not exists");
    }
    Java 6
public boolean moveFile(String sourcePath, String targetPath) {
    File fileToMove = new File(sourcePath);
    return fileToMove.renameTo(new File(targetPath));
}
Java 7(使用NIO)
public boolean moveFile(String sourcePath, String targetPath) {
    boolean fileMoved = true;
    try {
        Files.move(Paths.get(sourcePath), Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING);
    } catch (Exception e) {
        fileMoved = false;
        e.printStackTrace();
    }
    return fileMoved;
}
    您可以为该任务执行一个外部工具(例如copy在Windows环境中),但是为了保持代码的可移植性,通常的方法是:
File#renameTo只要源位置和目标位置在同一卷上,该文件就可以工作。我个人会避免使用它来将文件移动到其他文件夹。
试试这个 :-
  boolean success = file.renameTo(new File(Destdir, file.getName()));
    如果其中包含现有逻辑,则仅使用替换文件编写此方法以在我自己的项目上执行此操作。
// we use the older file i/o operations for this rather than the newer jdk7+ Files.move() operation
private boolean moveFileToDirectory(File sourceFile, String targetPath) {
    File tDir = new File(targetPath);
    if (tDir.exists()) {
        String newFilePath = targetPath+File.separator+sourceFile.getName();
        File movedFile = new File(newFilePath);
        if (movedFile.exists())
            movedFile.delete();
        return sourceFile.renameTo(new File(newFilePath));
    } else {
        LOG.warn("unable to move file "+sourceFile.getName()+" to directory "+targetPath+" -> target directory does not exist");
        return false;
    }       
}
    请尝试这个。
 private boolean filemovetoanotherfolder(String sourcefolder, String destinationfolder, String filename) {
            boolean ismove = false;
            InputStream inStream = null;
            OutputStream outStream = null;
            try {
                File afile = new File(sourcefolder + filename);
                File bfile = new File(destinationfolder + filename);
                inStream = new FileInputStream(afile);
                outStream = new FileOutputStream(bfile);
                byte[] buffer = new byte[1024 * 4];
                int length;
                // copy the file content in bytes
                while ((length = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, length);
                }
                // delete the original file
                afile.delete();
                ismove = true;
                System.out.println("File is copied successful!");
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
               inStream.close();
               outStream.close();
            }
            return ismove;
            }
    close指令位于一个finally块中,或者它使用try-with-resources块,则这将更加健壮。
                    你可以试试这个..干净的解决方案
Files.move(source, target, REPLACE_EXISTING);
    javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: REPLACE_EXISTING