Hi.
Solution 2 - 1 is not a great idea!
Also, try something like:
String fileName = request.getParameter("Id") + ".xml"; File toDel = new
File("c:\\", fileName); //get some info out.println("File exists:" +
toDel.exists()); out.println("File is readOnly: " + !toDel.canWrite()); If
(toDel.exists() && toDel.canWrite()) {
if (!toDel.delete()) {
Thread.sleep(1000); //try get around file lock/release
issue? (? Stab in the dark maybe!)
if (!toDel.delete() {
out.println("Failed to delete file - with
delay/pause and retry");
//could also tell it to delete on JVM exit - not
that useful with most web apps though
toDel.deleteOnExit();
}
}
} else {
out.println("Failed to delete file " + fileName + " - file does not
exist or is read only!"); }
You could also try and put the sleep and retry in a loop.
However, the main error in your code was that the datName reference when
creating a the File object was a string - when it should be a variable
reference! i.e. new File("datename") should be new File(datname);
Hope that helps,
Regards,
Carl
-----Original Message-----
From: Anne Milbert [mailto:[EMAIL PROTECTED]
Sent: 07 December 2005 16:13
To: Tomcat Users List
Subject: Howto delete a file with a servlet???
Hi anyone,
I'd like to delete a file with a servlet. I tried it with the delete()
function and with the runtime.exec() function. What am I doing wrong?
Here's my code:
Solution 1:
[...]
String cmd = "del C:\\" + request.getParameter("Id") + "-" + eleno + ".xml";
Runtime runtime = Runtime.getRuntime(); Process process =
runtime.exec(datname) [...]
Solution 2:
[...]
String datname = "C:\\" + request.getParameter("Id") + "-" + eleno + ".xml";
boolean success = (new File("datname")).delete(); if (!success) {
out.println("Couldn't delete file: " + datname); } [...]
Regards,
Anne
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]