---
title: "How can I rename a file in Java?"  
description: "How can I rename a file in Java?"  
author: "Anonymous User"  
published: 2013-10-22  
updated: 2013-10-23  
canonical: https://www.mindstick.com/forum/1703/how-can-i-rename-a-file-in-java  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# How can I rename a file in Java?

How can I [rename](https://www.mindstick.com/interview/23303/how-to-rename-extention-of-mdf-file) a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) from Java.io?

```
File temporaryFile = File.createTempFile();
temporaryFile.renameTo(newfileName);
```

And if newfileName is [exists](https://www.mindstick.com/forum/158947/how-do-you-use-the-exists-operator-to-check-for-the-existence-of-records-in-a-subquery) then its fails.

How do I do a force rename?

## Replies

### Reply by Donna Klinton

Hi there\
Thanks for your anwser.It's helpful.But i want to know is there any program for that?Thanks

### Reply by Samuel Fernandes

```
public void forceRename(File source, File target) throws IOException
{
   if (target.exists()) target.delete();
   source.renameTo(target)
}
```


---

Original Source: https://www.mindstick.com/forum/1703/how-can-i-rename-a-file-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
