Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

can't delete file.....

Posted on 2003-02-24
14
Medium Priority
?
252 Views
Last Modified: 2010-03-31
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) {

//#1
boolean success = (new File(str1)).delete();
     if (!success) {
     System.out.println("could not delete");
     }
       
         
//#2    
        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....
0
Comment
Question by:ticoldam12
13 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8012665
Is the file readonly?  (the one you are trying to delete)

That would cause the delete to fail...
0
 

Author Comment

by:ticoldam12
ID: 8012784
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
0
 
LVL 1

Expert Comment

by:tutran
ID: 8013016
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"
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:fivesigmaevent
ID: 8013022
Try using double backslash from this:

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

to this

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

Author Comment

by:ticoldam12
ID: 8013111
My dear friends...no 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
0
 

Author Comment

by:ticoldam12
ID: 8013131
hey hey....now it works boys
sorry fivesigmaevent......you are right......i forgot to to add one more backslash....
thank you all of you boys for your help
0
 

Author Comment

by:ticoldam12
ID: 8013169
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.
0
 
LVL 1

Accepted Solution

by:
tutran earned 400 total points
ID: 8013193
Do this:

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

0
 

Author Comment

by:ticoldam12
ID: 8013252
thank you tutran!!!!
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8013274
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 .
0
 

Expert Comment

by:markbrdsly
ID: 8017983
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.

import java.io.File;

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"
      if(fileOne.exists())
      {
         // Delete one file
         fileOne.delete();
      }
      // Then rename the other
      fileTwo.renameTo(fileOne);
   }
}

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!
0
 

Expert Comment

by:CleanupPing
ID: 9059102
ticoldam12:
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 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9671512
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

578 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