Problem in deleting file from java

M

meghab70

Hi all

I am using java to create a file in directory and after creating i am
using servletOutputStream to display it on web and once it is displayed
I want to delete that file from directory but it is not deleting file.
I am using following code

filePath = "C:/" + fileName;
FileInputStream fis = new FileInputStream(filePath);
int bytesAvailable = fis.available();
byte[] fileBytes = new byte[bytesAvailable];
fis.read(fileBytes);
response.setContentLength(fileBytes.length);
response.setContentType("application/pdf");
ServletOutputStream sos = null;
try
{
sos = response.getOutputStream();
sos.write(fileBytes);
sos.flush();
}
catch (Exception e) {
System.out.println("Exception"+e);
}
finally {
if (sos != null) {
sos.close();
File file = new File(filePath);
System.out.println("filePath"+filePath);
System.out.println("file"+file);
isdeleted = file.delete();
}

Please tell me how I can delete it.
 
G

Gordon Beaton

I am using java to create a file in directory and after creating i
am using servletOutputStream to display it on web and once it is
displayed I want to delete that file from directory but it is not
deleting file.

It's always a good idea to close any files when you're done with them,
which you haven't done here as far as I can tell.

But since you seem to be on Windows, you *must* close the file before
deleting it.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top