Sample Programs

Sample java program to rename file or directory

 package org.jusfortechies.example; 

 import java.io.File;
 
 public class RenameFile {

   public static void main(String[] args) {

     String oldFileName = "C:\\oldFile.txt";
     String newFileName = "C:\\newFile.txt";

     //File or directory with old name
     File file = new File(oldFileName);

     //File or directory with new name
     File file2 = new File(newFileName);

     //Rename file or directory
     boolean success = file.renameTo(file2);

     //If renaming fails
     if (!success) {
       System.out.println("Unable to rename file.");
     }

   }

 }




blog comments powered by Disqus