This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Couldn't move file just created earlier in the same program

 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am not able to move a text file immediately after having successfully created. Below is what the code snippet looks like:

Only the C:/Employee.txt has been moved to C:/Archived but not C:/NewEmployee.txt. Yet, it is possible to move C:/NewEmployee.txt to C:/Archived if the file is not created by this process. As a result, I need your feedback on the following areas wrt moving files:
( i ) How to unlock or free up C:/NewEmployee.txt prior to moving to C:\Archived. An alternative approach would be to create it under C:\Archived but it is needed in C:/ for other purpose which I won’t elaborate any further?
( ii ) How to overwrite C:/NewEmployee.txt if the file already exists in C:/Archived? Again, it is possible to delete the existing file first but would like to know whether the overwriting of file is possible on Windows platform?
Many thanks,
Jack

 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

These are local variables created in the if block. Try closing them after you call flush. These are not the same you close using the finally block.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John is right. Inside the try-block you are declaring local variables, whereas the ones you are closing are (probably) instance variables. These local variables never get closed.
 
Jack Bush
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

You were absolutely spot on even though it was not obvious to have picked that up. I solved it by moving the declaration of the two variables to the beginning of the main method so that both declarations and closing them are referencing the same variables.

Thank you very much,

Jack
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic