File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Renaming File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Renaming File" Watch "Doubt in Renaming File" New topic
Author

Doubt in Renaming File

Tom Hopkins
Greenhorn

Joined: May 23, 2007
Posts: 9
Hello everyone,
I am trying to rename file inside directory as mentioned in K&B book but its not "renaming" it.
Here is code..any help greatly appreciated!!!

File f1 = new File("newD");
f1.mkdir();
File f2= new File(f1,"file1.txt");
File f3 = new File(f1,"file2.txt");
PrintWriter pw2= new PrintWriter(f2);
PrintWriter pw3= new PrintWriter(f3);
pw2.println("This is File 1");
pw2.flush();
pw2.close();
pw3.println("This is File 2");
pw3.flush();
pw3.close();

File file1Rename = new File(f1,"dirFile1.txt");
File file2Rename = new File(f1,"dirFile2.txt");

f2.renameTo(file1Rename);
f3.renameTo(file2Rename);

Thanks
Chintan
--Most difficult thing in life is to make look things easy----
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

Your code works fine for me. Here's my complete program...

How are you "looking" at these files? If you're using something like Windows Explorer (which is quirky about this), are you refreshing your view?

What operating system are you using? If you're using a Unix type, you could use the "ls" command to list the directory contents from the command line. (On Windows, I'm not sure, but I think the command is "dir".)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tom Hopkins
Greenhorn

Joined: May 23, 2007
Posts: 9
Marc,
Thank you for your prompt reply...
yes, your code is running perfectly for me..i am wondering why my code isnt running(everything is same).
I am running this on Windows Platform..and i checked both through Windows Explorer as well as from command prompt..but still the file name doesnt get changed...anyways..you proved your point that it gets "renamed"..I will have to further research in my code as why its not "renaming" .
Thank you so much once again for your time.
Greatly appreciated!!!
Chintan.
--- The most difficult thing is to make look things easy---
Tom Hopkins
Greenhorn

Joined: May 23, 2007
Posts: 9
Marc,
I am sorry but i forgot to mention one thing, that after i write to file..i read from file and then rename it. I never closed the opened file.
Do you think that would cause the problem of not renaming the file as the file is still open(as i am reading from it).

Thank you for your assistance in advance
Regards,
Chintan
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi,
Close the file and try to rename it. It may work but not sure




Regards
Nik
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Tom Hopkins:
...i forgot to mention one thing, that after i write to file..i read from file and then rename it. I never closed the opened file. Do you think that would cause the problem of not renaming the file as the file is still open(as i am reading from it)...

That sounds like a good bet. Try experimenting with that, and see if you can get it working. If not, post the entire code and we'll have a look.

Also note that the renameTo method returns a boolean indicating whether the rename was successful. You might print this out as part of your troubleshooting.
[ May 24, 2007: Message edited by: marc weber ]
Tom Hopkins
Greenhorn

Joined: May 23, 2007
Posts: 9
Well, I did figure something new(but dont want to make a conclusion)
IT LOOKS LIKE MULTIPLE FILE OPERATIONS cannot be performed together(I was trying to create a simple program that had all the stuff for files create, delete, rename)...If you run all of these functionalities seperately in seperate files it works otherwise it would not work(if you have them all in one file). I tried deleting after renaming which did not work.

Thank you Marc, Nick for your attention!!!

Mark, reanmeTo() is returning false.

Here is the code
----------code-------------
File f1 = new File("newD");
f1.mkdir();
File f2= new File(f1,"file1.txt");
File f3 = new File(f1,"file2.txt");
PrintWriter pw2= new PrintWriter(f2);
PrintWriter pw3= new PrintWriter(f3);
pw2.println("This is File 1");
pw2.flush();
pw2.close();
pw3.println("This is File 2");
pw3.flush();
pw3.close();

FileReader fr2=new FileReader(f2);
BufferedReader br2 = new BufferedReader(fr2);
System.out.println("Contents of file 1: "+br2.readLine());

FileReader fr3=new FileReader(f3);
BufferedReader br3 = new BufferedReader(fr3);
System.out.println("Contents of file 2: "+br3.readLine());

//RENAMING/*Code stops working from here--although println() works*/
File file1Rename = new File(f1,"dirFile1.txt");
File file2Rename = new File(f1,"dirFile2.txt");


System.out.println("rename file 1 "+f2.renameTo(file1Rename));
System.out.println("Rename file 2 "+f3.renameTo(file2Rename));

//DELETING FILES
System.out.println("Deleting file 1.....");
f2.delete();
System.out.println("Deleting file 2.....");
f3.delete();
-----OUTPUT---
Contents of file 1: This is File 1
Contents of file 2: This is File 2
rename file 1 false
Rename file 2 false
Deleting file 1.....
Deleting file 2.....

Any help as usual greatly appreciated and thanks in advance!!!

Regards,
Chintan
Michael Raymond Jr.
Ranch Hand

Joined: May 16, 2005
Posts: 178
I'm not very good wtih file IO (or anything java for that matter), but I think it's because f1 is a relative directory, not absolute.

I created a file sorter a long time ago and here's what I iused to rename files:


///////////
String currentDir = System.getProperty("user.dir"); //gets abs path of current directory

String newDir = currentDir+"\\"+documents; //appends file name to abs directory

boolean success1 = fileName.renameTo(new File(newDir, fileName.getName()));
\\\\\\\\\\\\

The only difference between your code and mine, really, is that my 'newDir' variable refers to the absolute directory path. So perhaps when your files are being renmaed it might be placing them in an arbitrary location, like, C root or something. Try and do a search on your system for what you think the file name is supposed to be and see if it turns up somewhere...i had that happen to me when I was coding my file sorter.

--------didn't see your response before i posted ;-)
[ May 24, 2007: Message edited by: Michael Raymond Jr. ]

Scooby Snacks for everyone...<br /> <br />SCJA, SCJP 1.4
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Try closing your FileReaders (FileReader fr2, FileReader fr3) before renaming/deleting the files.

As far as I can see it, you won't be able to modify them as long as the 'files are open' (non java terminology, but i think that is what happens).

[EDIT] I meant FileReaders, not the BufferedReaders (though closing them before the FileReaders would be cleaner).
[ May 24, 2007: Message edited by: Sergio Tridente ]

SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Tom,
Just look at the below code what you have posted

File file1Rename = new File(f1,"dirFile1.txt");
File file2Rename = new File(f1,"dirFile2.txt");


System.out.println("rename file 1 "+f2.renameTo(file1Rename));
System.out.println("Rename file 2 "+ f3.renameTo(file2Rename));

"file1Rename" and "file2Rename" for the files which are refering should exist. So just add this modified code and check out whether it works.I am not sure.



File file1Rename = new File(f1,"dirFile1.txt");
File file2Rename = new File(f1,"dirFile2.txt");
file1Rename.createNewFile();
file2Rename.createNewFile();

System.out.println("rename file 1 "+f2.renameTo(file1Rename));
System.out.println("Rename file 2 "+ f3.renameTo(file2Rename));


Regards
Nik
Tom Hopkins
Greenhorn

Joined: May 23, 2007
Posts: 9
Thank you everyone for your response.

Nik, the option you provided created 2 new files instead of renaming them(so now there are 4 files in directory).
Sergio , your option worked!!! I closed the readers and it was able to rename. Delete option works too fine.Thanks Sergio.

Thank you everyone once again for all of your help.

Regards,
Chintan
"Logic is in the eye of the logician."
- Gloria Steinem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Renaming File