Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 14 May 2015

How to rename the file using Java

Following example shows how to rename the file using file.renameTo() method in Java.

renameTo
public boolean renameTo(File dest) 
Renames the file denoted by this abstract pathname. Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.
Note that the Files class defines the move method to move or rename a file in a platform independent manner.
Parameters: 
dest - The new abstract pathname for the named file
Returns:
true if and only if the renaming succeeded; false otherwise
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to
either the old or new pathnames
NullPointerException - If parameter dest is null

Sample Program
FileRename.java
01.package com.javatutorialcorner.file;
02. 
03.import java.io.File;
04. 
05.public class FileRename {
06. 
07. public static void main(String[] args) {
08.  File jtcFile = new File("/home/annamalai/workspace/File/jtc.txt");
09.  File javaTutorialCornerFile = new File(
10.    "/home/annamalai/workspace/File/javatutorialcorner.txt");
11.  if (jtcFile.renameTo(javaTutorialCornerFile)) {
12.   System.out
13.     .println("jtc.txt File renamed to javatutorialcorner.txt");
14.  } else {
15.   System.out.println("Error");
16.  }
17. 
18. }
19. 
20.}

Result
The above code will produce the following output.

jtc.txt File renamed to javatutorialcorner.txt

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Java7 – More precise Re-throw of an ExceptionIn this tutorial we are going to see re-throw exception in Java7.In previous versions of Java re-throwing exception in catch block didn’t indicate the actual exceptions possible from the try block.Also you could not change th… Read More
  • How to Convert JSON String to Object using GSONIn this Tutorials we are going to see how to convert JSON String to Java object using GSON library. 1. Create project called JSONExample. 2. Create package called com.javatutorialscorner.gson  3. Create java class ca… Read More
  • Sending Email with AttachmentIn this tutorials we are going to see how to send the email with attachment using java program.I used gmail SMTP server for sending email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package … Read More
  • How to Create JSON Data using JSONSimple JSON – JavaScript Object Notation JSON is a simple data exchange format for read and write data.JSON is the best alternative for XML data.We can create JSON data using Jackson, Google Gson, JSON.simple libraries. In this … Read More
  • How to Convert String to InputStreamIn this tutorials we are going to see how to convert String to InputStream using methods available in Java IO. BufferedReader is used to get String again from InputStream. Create project called JavaMisc. Create package ca… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to rename the file using Java Rating: 5 Reviewed By: eHowToNow