aspose file tools*
The moose likes I/O and Streams and the fly likes Having a problem in renaming a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Having a problem in renaming a file" Watch "Having a problem in renaming a file" New topic
Author

Having a problem in renaming a file

Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
Hai friends , i am having a problem in renaming a file . when i used renameTo() method i am not able to rename it , the code complies without any error but i go and look in to my specified path i see the filename remains same . i am stating the code below



import java.io.*;

public class FileMethod {


public static void main(String[] args)
{
// TODO Auto-generated method stub

File f1 = new File("f://Parent/child/sairam.txt");
File f2 = new File ( " f://Paremt/child/sai.txt");
System.out.println("The file name" + f1.getName());
System.out.println("The parent" + f1.getParent());
System.out.println("The file path " + f1.length() );
System.out.println(" Last modified " + f1.lastModified());
System.out.println(" Is that a hidden file " + f1.isHidden());
System.out.println(" Is that a file " + f1.isFile());
f1.renameTo(f2);
System.out.println(" Rename file " + f2 );

}
}


Output


The file name :sairam.txt
The parentf:\Parent\child
The file path 0
Last modified 0
Is that a hidden file false
Is that a file false
Rename file f:\Paremt\child\sai.txt

As i told you eariler the code complies gives a correct output but
the file name in that directory is never changed(i mean it remains sairam.txt).
[ August 01, 2008: Message edited by: Bear Bibeault ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Originally posted by Guna Ranjan:
As i told you eariler the code complies gives a correct output


That doesn't matter if the code isn't being told to do the right thing.
Does the path for f2 exist or is it a misspelling?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
Sir , i have double checked there is no miss spelled word
all i want is to replace the sairam.txt in to sai.txt.. that is
i want the content to be same and to replace the filename alone !
i hope you mean it sir.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Really? "Paremt" is the same as "Parent" ?


[Jess in Action][AskingGoodQuestions]
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
oho i havent looked that! need to review it again sir
nice power observation !
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
Sir still not working ! see my code down bellow even after
correcting the spelling !



Code :

import java.io.*;

public class FileMethod {




public static void main(String[] args) {
// TODO Auto-generated method stub

File f1 = new File("f://Parent/child/Sairam.txt");
File f2 = new File ( " f://Parent//child//sai.txt");
System.out.println("The file name " + f1.getName());
System.out.println("The parent" + f1.getParent());
System.out.println("The file path " + f1.length() );
System.out.println(" Last modified " + f1.lastModified());
System.out.println(" Is that a hidden file " + f1.isHidden());
System.out.println(" Is that a file " + f1.isFile());
f1.renameTo(f2);
// f1.delete();
System.out.println(" Rename file " + f2 );
//System.out.println("Deleting the file " + f1);

}

}

OutPut:

The file name Sairam.txt
The parentf:\Parent\child
The file path 23
Last modified 1217605550000
Is that a hidden file false
Is that a file true
Rename file f:\Parent\child\sai.txt
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Try removing the space before f in the second file:

should become

I tried your code first and it failed. I removed the space and it worked.

Also, the deleting of f1 will not work at all since f1 will not be present anymore - it is renamed after all.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
Great Job sir !
finally with help of you !
i have no words to express!


thank you very much sir
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

You're welcome.
 
 
subject: Having a problem in renaming a file