Solved

Rename files in the same folder

Posted on 2007-03-20
2
224 Views
Last Modified: 2010-03-31
I want to rename some of the files using Java code.

The path of the folder is specified. I have the following code to rename the files

        String sourcePath = "E:\\Test";
        File filePath = new File (sourcePath );

         File[] listFiles = filePath.listFiles();
         for (int i=0; i < listFiles.length; i ++) {
               System.out.println("Old File Name is: "+listFiles[i].getName());
               String newFileName = listFiles[i].getName().replaceAll("1", "2");
               System.out.println("New File Name:  "+newFileName);
                                        
               boolean renameSuccess = listFiles[i].renameTo(new File(newFileName));
                    
               if(renameSuccess) {
                     System.out.println("Rename of file: "+listFiles[i].getName()+" to: "+newFileName+" succeded\n" );
               } else {
                     System.out.println("Rename of file: "+listFiles[i].getName()+" to: "+newFileName+" FAILED\n" );
                     }
               }
         }


Rename always fails when I use this code. But when I specify a different target folder, the rename succeeds. Say, if I do

boolean renameSuccess = listFiles[i].renameTo(new File("C"\\test\\"+newFileName));

it owrks fine. May be since there is already a handler to the File object, rename fails.
Is there a way to rename the files in the same folder. Depending on some condition (which I have not specified in the code above) some files will be renamed while others will not be.

I am using J2se 1.4
0
Comment
Question by:thomas908
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18755164
try:

  boolean renameSuccess = listFiles[i].renameTo(new File(listFiles[i].getParentFile(), newFileName));
0
 
LVL 4

Expert Comment

by:bhaskar20001
ID: 18755497
Try the below modification in your code..

String[] listFiles = filePath.list();
for (int i=0; i < listFiles.length; i ++) {
           System.out.println("Old File Name is: "+listFiles[i]);
            String newFileName = listFiles[i].replaceAll("1", "2");
            System.out.println("New File Name:  "+newFileName);
            File fileOut = new File(sourcePath+"\", fileName);
             fileOut.renameTo(new File(sourcePath+"\", newFileName));    
 }

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Timer (static) 9 43
print map entry 34 69
tomcat startup error 5 65
Java: The Public Class Main 4 16
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question