?
Solved

can't delete file.....

Posted on 2003-02-24
14
Medium Priority
?
250 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
[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
14 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
Technology Partners: 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

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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