Unable to delete a file

Hi All,

  I found couple of posts similar to mine, but didn't find a solution. 

  I am trying to write to a file and if it fails, i want to delete it.

  Issue: As the file was not properly closed, i am not able to delete it (in my catch block). I tried to close the file with proper input (in catch block) but it throws an exception. I am using services from PSUtils

  Any help would be appreciated.


Can you post the service steps or the Java code being used? Perhaps we can see what needs to be tweaked to close the file on failure so it can be deleted.

What is the exception that is thrown?

Thanks for your reply Reamon.

Please find screen shot below. I had to put a try catch block to catch ps.util.io.close service exception.

The same works in my try block, where i write to a file and successfully close it.
sc.bmp (89.8 KB)
sc1.bmp (227 KB)

The issue is that the stream object created and used in the try block is not directly accessible in the catch block. In Developer, it looks like you’re passing in the stream object to close but at run-time it isn’t in the pipeline directly.

There are two approaches that you can use.

  1. Declare the stream object outside of the try block. Assign it a null value. Then it will be available directly in the catch block, with the stream var updated as appropriate from open/write.

  2. In the catch block, variables that were in the pipeline at the time of the exception are stored in the lastError/pipeline. You can pass lastError/pipeline/stream to the close service.

A 3rd possibility might be to lump all this file interaction into a single Java service. Normally I’m an advocate of implementing things in FLOW but this is one of those cases where the granularity of the PS Java services may be too fine.

If you just need to write some data to a file you might consider using a writeFile service that opens, writes, closes/deletes on error. There are a couple of these posted in the forums and in the Shareware section I think (they probably don’t delete on error but you can add that). You can also find one at my blog which you can access via the link in my signature block below. Start with one of these and modify as you see fit.


Hi Reamon,

      Your solution worked. I had to use steps 1 & 2 above to get it working.


You shouldn’t have had to use 1 and 2 above. One or the other should have been sufficient. Can you post your new steps?

You are right. I just need to use step 1.