Solved

Rename files in the same folder

Posted on 2007-03-20
2
219 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now