on 03-12-2008 3:26 PM
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???
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
User | Count |
---|---|
89 | |
10 | |
9 | |
9 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.