File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes File I/O Operations 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 "File I/O Operations" Watch "File I/O Operations" New topic

File I/O Operations

Mckenzie John

Joined: Sep 21, 2012
Posts: 21

i need to understand the file I/O operations in the following code

package com.scjp;

[Anayonkar]: Added code tags

I have added my questions against the lines as comment. Please help me understanding the concepts.

R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375

That is happening because, when you are renaming your file to a different file object, then a new file is created, content of old file / directory is copied there, and the old file/directory is deleted.

So, after the rename operation, your directory referenced by `file` object doesn't exist.

Try using this: -

Chan Ag

Joined: Sep 06, 2012
Posts: 1089
Try changing your code as follows.


And delete myDir and newDirName ( physical directories and files) and run this program twice and note the difference. That will answer your first and second question.

As for the last question, I think the original file/dir references are not deleted after we rename a file/directory. In any case a File object is just a file ( directory or file ) name that exists even after a rename. The old reference has a name ( file/directory name) but the actual physical dir/file as such does not exist cause it has been renamed. It is the new reference that is now mapped with the physical file/direcory after the rename. So a file or directory reference variable would still have the same name although the actual file/directory may not exist.
I agree. Here's the link:
subject: File I/O Operations
It's not a secret anymore!