You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Anne Milbert <an...@freenet.de> on 2005/12/07 17:13:22 UTC

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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Howto delete a file with a servlet???

Posted by Mauricio Nuñez <ma...@chile.com>.
El mié, 07-12-2005 a las 17:13 +0100, Anne Milbert escribió:
> 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();

Are you kidding???  Remove the quotes to new File("datname").


Mauricio




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Howto delete a file with a servlet???

Posted by Carl Olivier <ca...@unysen.co.uk>.
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:anne.milbert@freenet.de] 
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: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org