• Post Reply Bookmark Topic Watch Topic
  • New Topic

why is it that, i can not delete the file ?  RSS feed

 
k kju
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code below demonstrates simple 'copying operation' using buffered technique,


in the first catch clause statement, i can not delete the destination file if the source file does not exist. i doubt that,it failed because i wrap the 'File' into a stream? if so,how can I deal with it.
In addition,I can delete a file successfully if i just using 'file.delete',such as:
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are your log statements ? Printing log messages with real detailed stack trace information might tell you why you are unable to delete the file.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k kju wrote:in the first catch clause statement, i can not delete the destination file if the source file does not exist

That's probably because you haven't closed the stream (you don't close it until the finally block which is executed after the exception block).
Swap lines 16 and 17 around. That way, if your source file doesn't exist, the destination file won't be created and you won't have to delete it in the exception.
 
k kju
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
k kju wrote:in the first catch clause statement, i can not delete the destination file if the source file does not exist

That's probably because you haven't closed the stream (you don't close it until the finally block which is executed after the exception block).
Swap lines 16 and 17 around. That way, if your source file doesn't exist, the destination file won't be created and you won't have to delete it in the exception.


yes,that's right,to tell you the truth, I first put the line'BufferedReader' before the line'BufferedWriter',and I swap the line '16' and '17' intentionally,I just want to see what happened.
and it relevant with 'closing stream'.and if I place a statement 'File.delete' line at the finally clause after closing bw,and it works.
any way,thanks for response.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!