ファイルをある場所から別の場所にどのように移動しますか?プログラムを実行すると、その場所で作成されたファイルは自動的に指定された場所に移動します。どのファイルが移動されたかを知るにはどうすればよいですか?
回答:
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のIOを使用することができFileUtils.moveFileを
エラーが発生すると、がスローされるIOExceptionため、例外がスローされない場合は、ファイルが移動されたことがわかります。
File.renameToJavaからIOを使用して、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;
}
    (copyWindows環境のように)そのタスクのために外部ツールを実行することもできますが、コードを移植可能に保つための一般的なアプローチは次のとおりです。
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