aspose file tools*
The moose likes Java in General and the fly likes Couldn't move file just created earlier in the same program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Couldn Watch "Couldn New topic
Author

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

Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
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
Bartender

Joined: May 31, 2011
Posts: 1776

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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jack Bush
Ranch Hand

Joined: Oct 20, 2006
Posts: 235
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Couldn't move file just created earlier in the same program