Solved

Rename files in the same folder

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 85
eclipse apache tomcat admin console 52 153
tomcat startup error 5 136
Android development question 2 79
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…
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.
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

749 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