• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Renaming File

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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----
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".)
 
Tom Hopkins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Close the file and try to rename it. It may work but not sure




Regards
Nik
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. ]
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic