• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File I/O Operations

 
Mckenzie John
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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.

Thanks,
Mckenzie
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1089
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing your code as follows.



and



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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic