Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java File renameTo() Method

Java File renameTo() Method

Java File Java File

renameTo() is a method in Java's java.io.File a method provided by the class, used to rename a file or directory. This method can also be used to move a file to another directory (if the target path differs from the source path).

Method Syntax

public

boolean

renameTo

(

File

dest

)

Parameter Description

Return Value


Use Cases

renameTo() The method is typically used in the following situations:

  1. Rename a file
  2. Move a file to a different directory
  3. Rename a directory
  4. Move a directory to a different location

Notes

  1. The behavior of this method is platform-dependent
  2. In some operating systems, it may not be possible to move a file from one file system to another
  3. If the target file already exists, the operation may fail
  4. This method does not guarantee an atomic operation

Code Example

Example 1: Basic file renaming

Example

import

java.io.File

;

public

class

RenameExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

File

oldFile

=

new

File

(

"old_name.txt"

)

;

       

File

newFile

=

new

File

(

"new_name.txt"

)

;

       

       

if

(

oldFile.

renameTo

(

newFile

)

)

{

           

System

.

out

.

println

(

"File renamed successfully"

)

;

       

}

else

{

           

System

.

out

.

println

(

"File rename failed"

)

;

       

}

   

}

}

Example 2: Move a file to a different directory

Example

import

java.io.File

;

public

class

MoveFileExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

File

sourceFile

=

new

File

(

"source.txt"

)

;

       

File

destFile

=

new

File

(

"new_directory/source.txt"

)

;

       

       

if

(

sourceFile.

renameTo

(

destFile

)

)

{

           

System

.

out

.

println

(

"File moved successfully"

)

;

       

}

else

{

           

System

.

out

.

println

(

"File move failed"

)

;

       

}

   

}

}


Common Problems and Solutions

Problem 1: Rename operation returns false

Possible reasons: 1. Source file does not exist 2. Target file already exists 3. Insufficient permissions 4. Cross-file-system operation

Solutions:

  1. Check whether the source file exists
  2. Check whether the target file exists; delete it first if necessary
  3. Check file permissions
  4. Consider using other methods such as Files.move()

Problem 2: Cross-file-system operation fails

In Java 7 and above, you can useFiles.move()method as an alternative:

Example

import

java.nio.file.*

;

public

class

FilesMoveExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path source

=

Paths.

get

(

"source.txt"

)

;

        Path target

=

Paths.

get

(

"new_directory/source.txt"

)

;

       

       

try

{

            Files.

move

(

source, target, StandardCopyOption.

REPLACE_EXISTING

)

;

           

System

.

out

.

println

(

"File moved successfully"

)

;

       

}

catch

(

IOException

e

)

{

           

System

.

out

.

println

(

"File move failed: "

e.

getMessage

(

)

)

;

       

}

   

}

}


Best Practices

  1. Always check the return value to confirm whether the operation succeeded
  2. Consider using the method introduced in Java 7Files.move()which provides more options and better cross-platform support
  3. For important operations, consider adding appropriate exception handling
  4. Before moving a file, check whether the target path exists

Performance considerations


Summary

File.renameTo()is a simple but limited file renaming and moving method. Although it is suitable for basic scenarios, for complex file operations, especially those requiring cross-file-system operations, it is recommended to use the method introduced in Java 7Files.move()which provides more reliable behavior and more options.

Java File Java File