คุณจะย้ายไฟล์จากที่หนึ่งไปยังอีกที่หนึ่งได้อย่างไร? เมื่อฉันเรียกใช้โปรแกรมของฉันไฟล์ใด ๆ ที่สร้างขึ้นในตำแหน่งนั้นจะย้ายไปยังตำแหน่งที่ระบุโดยอัตโนมัติ จะทราบได้อย่างไรว่าไฟล์ใดถูกย้าย
คุณจะย้ายไฟล์จากที่หนึ่งไปยังอีกที่หนึ่งได้อย่างไร? เมื่อฉันเรียกใช้โปรแกรมของฉันไฟล์ใด ๆ ที่สร้างขึ้นในตำแหน่งนั้นจะย้ายไปยังตำแหน่งที่ระบุโดยอัตโนมัติ จะทราบได้อย่างไรว่าไฟล์ใดถูกย้าย
คำตอบ:
myFile.renameTo(new File("/the/new/place/newName.file"));
File # rename เพื่อทำเช่นนั้น (ไม่เพียง แต่สามารถเปลี่ยนชื่อได้ แต่ยังย้ายไปมาระหว่างไดเร็กทอรีอย่างน้อยก็ในระบบไฟล์เดียวกัน)
เปลี่ยนชื่อไฟล์ที่แสดงโดยชื่อพา ธ นามธรรมนี้
ลักษณะการทำงานหลายประการของวิธีนี้ขึ้นอยู่กับแพลตฟอร์มโดยเนื้อแท้: การดำเนินการเปลี่ยนชื่ออาจไม่สามารถย้ายไฟล์จากระบบไฟล์หนึ่งไปยังอีกระบบหนึ่งอาจไม่ใช่ atomic และอาจไม่สำเร็จหากไฟล์ที่มีชื่อพา ธ นามธรรมปลายทาง มีอยู่แล้ว. ควรตรวจสอบค่าส่งคืนเสมอเพื่อให้แน่ใจว่าการดำเนินการเปลี่ยนชื่อสำเร็จ
หากคุณต้องการโซลูชันที่ครอบคลุมมากขึ้น (เช่นต้องการย้ายไฟล์ระหว่างดิสก์) ให้ดูที่ 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.renameToจาก Java 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;
}
    คุณสามารถเรียกใช้เครื่องมือภายนอกสำหรับงานนั้นได้ (เช่น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