• 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

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

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic