• 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

a question about File class(urgent)

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider the following code:
File s=new File("myfile.txt");
s.renameTo("myfile1.txt");
According to the java API of File class "Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change."
but the renameTo method does chang the Instances of the File "s" if the method return true.
maybe we can say it is equals to below:
File s=new File("myfile.txt");
s=new File("myfile1.txt");
We assume that no other references point to s.Can i say that the object created by new File("myfile.txt") is eligible for GC collection after s.renameTo("myfile1.txt") has been executed.Can anybody tell me if i am right or wrong,please! Thanx in advance.

------------------
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I am following your question correctly:
File s=new File("myfile.txt");
s.renameTo("myfile1.txt");
// s now refers to a file that does not exist because the name was changed
// you would have to do this:
s = new File("myfile1.txt") ;
// in order to refer to the file by its new name.
Bill
 
william shen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry it should be s.renameTo(new File("myfile1.txt"));
I assume that the "myfile1.txt" exists.what i mean is after
s.renameTo(new File("myfile1.txt")); has been executed,if the object created by new File("myfile.txt") is eligible for GC ,thus
if s.renameTo(new File("myfile1.txt")); is equal to s=new File("myfile1.txt")

Originally posted by William Brogden:
If I am following your question correctly:
File s=new File("myfile.txt");
s.renameTo("myfile1.txt");
// s now refers to a file that does not exist because the name was changed
// you would have to do this:
s = new File("myfile1.txt") ;
// in order to refer to the file by its new name.
Bill


------------------

[This message has been edited by william shen (edited June 28, 2001).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi william,
I think that the rename method will probably replace the object in memory with the new File object. Therefore, there would be no object to GC.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see nothing in the source code for File.java or FileSystem.java that indicates that rename creates any new object. As far as I can tell it simply renames the file referred to.
I think the File objects are not changed, only the actual file system file name for a particular file.
s.renameTo(new File("myfile1.txt"));
// certainly creates a new File object that is immediately GCable
Bill
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RHE says that File class is little more than a class with String that specifies the file path. Maybe that is what they meant by immutable?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic