?
Solved

Rename files in the same folder

Posted on 2007-03-20
2
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

743 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