can't delete file.....

hi boys....
im using this code:

assume that str1 = "C:\info.txt" and str2 = "c:\temp.txt"
What im trying to do is to delete info.txt and rename temp.txt to info.txt

public void make_changes (String str1, String str2) {

boolean success = (new File(str1)).delete();
     if (!success) {
     System.out.println("could not delete");
        File old = new File(str2);
     File new = new File(str1);

        boolean success1 = old.renameTo(new);
     if (!success1) {

     System.out.println("not renamed");

this code can not delete the file info.txt and also can not rename temp.txt to info.txt.
Why this code can't do this????
Do i have to add something else????
Please help me....
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is the file readonly?  (the one you are trying to delete)

That would cause the delete to fail...
ticoldam12Author Commented:
both files are not read-only....
the code i gave above should work...but it is not working
...i dont know why....
If somebody has another idea on how to delete a file and rename another one...please let me know it
I don't think you can specify a string with single back slash.  You have to do either use double back slash such as "C:\\temp.txt" or single forward slash "C:/temp.txt"
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Try using double backslash from this:

str1 = "C:\info.txt";
str2 = "c:\temp.txt";

to this

str1 = "C:\\info.txt";
str2 = "c:\\temp.txt";
ticoldam12Author Commented:
My dear way...
I specified all possible ways to do it
1- /
2- \
3- \\
4- //

and nothing...if there is another way to to something like this...tell me
ticoldam12Author Commented:
hey it works boys
sorry are right......i forgot to to add one more backslash....
thank you all of you boys for your help
ticoldam12Author Commented:
anther question...
Im getting the path of the file info.txt from a FileDialogBox. The string it gets is on this format for example "c:\info.txt"

Are there a procedure to add one more backslash to a given path?
Now, i need to add one more backslash to every backslash separator.
Do this:

str1.replace('\\', File.separatorChar)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ticoldam12Author Commented:
thank you tutran!!!!
Can you not get your file in this format "c:/info.txt", then you would need to append and its vaild string . However  "c:\info.txt" is not a valid String, because '\' is java escape character, so always need to put '\' before special character to make a vaild string .
Try this, it may not be really what you want and I cannot explain why it works - the only thing I have changed is to use an overloaded constructor for the File objects allowing me to identify the file and the directory it is in separately and explicitly - but I do know that it should.


public class RenameTest {
   public static void main(String[] args) {
      // I do not know why but, if you use this
      // overloaded constructor to create an
      // instance of the File class, then you
      // should be able to delete one file and
      // rename the other.
      File fileOne = new File(
                     "C:/WINDOWS/TEMP", "fileone.txt");
      File fileTwo = new File(
                     "C:/WINDOWS/TEMP", "filetwo.txt");
      // You could just as easilly use the back slash
      // separator, but remember to escape that
      // character thus; "C:\\WINDOWS\\TEMP"
         // Delete one file
      // Then rename the other

If this does not work correctly, check that you have the correct permissions to perform such operations on the files. Finally, try to make sure that neither file is actually being used by another application for this could 'lock' that file.

PS the above compiles and works correctly on an MS Windows platform in version 1.4.1 of Sun's Java SDK. Good luck!
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept tutran's comment as answer

Please leave any comments here within the next seven days.


EE Cleanup Volunteer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.