Rename file or an empty directory - Java File Path IO

Java examples for File Path IO:Directory

Description

Rename file or an empty directory

Demo Code


import java.io.*;
 
public class Main {
 
  public static void main(String[] args) {
    File oldName = new File("C://Folder//source.txt");
    File newName = new File("C://Folder//destination.txt");
     boolean isFileRenamed = oldName.renameTo(newName);
   //  w ww. jav a  2  s. co  m
     if(isFileRenamed)
       System.out.println("File has been renamed");
     else
       System.out.println("Error renaming the file");
  }
}

Result


Related Tutorials