Solved

Rename files in the same folder

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Starting to learn JAVA, 7 60
Should localization be done inside spring controller 5 12
even odd program using while loop 3 31
varialbe initialization 11 30
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 …

863 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

23 Experts available now in Live!

Get 1:1 Help Now