GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Private Policy
  • Contact
  • Java
  • Java Core
  • Java Lambda Expression
  • Java Fundamentals
  • Java Stream
  • Java Tools
  • Java XML
  • Jakarta EE
  • CDI
  • JSON Binding
  • JSON Processing
  • MVC

This view

  • Original page
  • New address

GeezerVison is showing huongdanjava.com — Move file in Java - Huong Dan Java

Khanh Nguyen

I love Java and everything related to Java.

Follow

Share this post

Tags

  • Java File
  • Java Files
Views: 1,142

To migrate any file in Java, in Java 6 or older Java versions, we can use the renameTo() method located in the File object to do this:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.huongdanjava.javaexample;
 
import java.io.File;
 
public class MoveFile {
 
    public static void main(String[] args) {
        File source = new File("F://source.txt");
        File dest = new File("E://dest.txt");
 
        if (source.renameTo(dest)) {
            System.out.println("Move file " + source.getAbsolutePath() + " to " + dest.getAbsolutePath() + " success.");
        }
    }
}

Result:

Java
1
Move file F:\source.txt to E:\dest.txt success

From Java 7 onwards, we have another way to move a file, which is to use the Files object. This Files object contains all the methods that make it possible to perform all file-related operations such as creating files, deleting files, copying files or moving files, etc. Below is the code to move a file with Files object:

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.huongdanjava.javaexample;
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
public class MoveFile {
 
    public static void main(String[] args) throws IOException {
        Path source = Paths.get("F://source.txt");
        Path dest = Paths.get("E://dest.txt");
 
        Files.move(source, dest);
    }
}

And like copying files, we can even use the Apache Commons IO library to move a file.

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.huongdanjava.javaexample;
 
import java.io.File;
import java.io.IOException;
 
import org.apache.commons.io.FileUtils;
 
public class MoveFile {
 
public static void main(String[] args) throws IOException {
File source = new File("F://source.txt");
File dest = new File("E://dest.txt");
 
FileUtils.moveFile(source, dest);
}
}



You might also like...

  • What is a Database Transaction? ACID, JDBC & Isolation Levels

    What is a Database Transaction? ACID, JDBC & Isolation Levels

  • What is JDBC? JDBC Architecture, Driver, Connection, Statement & PreparedStatement | Java

    What is JDBC? JDBC Architecture, Driver, Connection, Statement & PreparedStatement | Java

  • Java Primitive Data Types Part 2 | float, double, boolean

    Java Primitive Data Types Part 2 | float, double, boolean

  • Java Primitive Data Types Part 1 | byte, short, int, long, char & Type Conversion

    Java Primitive Data Types Part 1 | byte, short, int, long, char & Type Conversion

Previous Post: Expose SOAP Web Service using CXF component in Mule ESB
Next Post: Basic about JDBC

Add Comment Cancel reply

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.