cancel
Showing results for 
Search instead for 
Did you mean: 

File in filesystem locked for deletion

Former Member
0 Kudos

Hi,

I have a problem with the deletion of a file from the filesystem.

I have created a file with:

File file = new File("download.csv");

then fill the file through a RFC call to the backend and a third party CSV Writer.

Finally I want to transform the file for a download an delete it.

I transform the file with:

try {
     IWDResource resource = WDResourceFactory.createResource(
			new FileInputStream(file),
			CustomerRFCView.FILE_NAME,
			CustomerRFCView.FILE_EXT,
			true);
wdContext.currentContextElement().setFileDownloadResource(resource);
} catch (FileNotFoundException e) {
    wdComponentAPI.getMessageManager().reportMessage(
                                                 IMessageCcCustomerRFC.FNF,
			new Object[] {
				wdContext
				.currentContextElement()
				.getFileDownloadResource()
				.getResourceName()},
			true);
}

and finally I want to delete the file from the filesystem with:

file.delete();

.

But the file is not deleted. If I try to delete it manually Windows is saying that the file is in use and can not be deleted.

If I comment the download transform part, then the file is deleted from the server.

Can anybody help me???

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi.

file.delete() don't work when you still have references to the file.

In your case I would say you have a FileInputStream with reference to your file, stored in the context.

If you delete the resource entry in the context, you might be more successful deleteing the file.

However, since you are just creating the file and delete it after download, I would suggest you create a StringBufferInputStream in the memory instead. (ByteArrayInputStream if it's a binary file)

Regards, Mikael

Former Member
0 Kudos
ByteArrayOutputStream bout = new ByteArrayOutputStream();
		try {
			InputStream in = new FileInputStream(file);

			byte buffer[] = new byte[4096];

			int read = 0;
			do {
				read = in.read(buffer);
				if (read != -1) {
					bout.write(buffer, 0, read);
				}
			} while (read != -1);

			//copy binary to an input stream for reading and parsing
			//save this for multiple uses
			in.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		//read file and store binary in bout
		InputStream input = new ByteArrayInputStream(bout.toByteArray());

		IWDResource resource = WDResourceFactory.createResource(
								input, 
								CustomerRFCView.FILE_NAME, 
								CustomerRFCView.FILE_EXT, 
								true);
		wdContext.currentContextElement().setFileDownloadResource(resource);

Answers (0)