• Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete a File ?

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

How can i delete a file , in which file name has a space ?
Using java.io.File Class.

Ex: if file name is like "Test File.txt" how can i delete
File.delete is not deleting this file ( having the space) please help.
 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In fact i tested this out without any problems.
The file is getting deleted
The code i used is:

import java.io.*;
class FileDelete
{
public static void main(String[] args)
{

File file = new File("D:\\Test File.txt");
boolean bExists = file.exists();
System.out.println("File exists:"+bExists);
if(bExists)
{
boolean bDelete = file.delete();
System.out.println("File Deleted:"+bDelete);
}

}
}

Usually the File delete fails if you have opened a Input / Output stream to the file & you try to delete the file before closing the stream
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sajee Joseph,

Some thing is happening Fishy. Yep It's working now.
Y'day i dint even get any exception when i execute File.delete(), but the file was not deleted. But now it works fine i have to chk the reson behind it.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible that another process had the file open, and thus locked, though I'd expect an exception in that case. However, when deleting files via Ant scripts that are locked in this manner, I find Ant doesn't give an error message, so maybe there is no exception.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible that another process had the file open, and thus locked, though I'd expect an exception in that case.

No, that would've implied that some careful thought and design went into this API. Instead, what we have is a method which simply returns false if it fails. No other info is provided. Frankly, it's one of the worst methods I know of in the standard API's which hasn't been deprecated. Similar problems exist for other File methods like renameTo() and mkdir().

Reasons these might fail (with no exception) include: another process or thread has the file open currently, you don't have the correct access priviliges, the file/directory you specified doesn't exist (perhaps because its path is incorrect). Several of these conditions can be checked using File methods like exists() and canWrite(). And maybe FileChannel's tryLock(). However if your own Java program has left an open FileInputStream or FileOutputStream (or similar IO class) then these mehtods will usually fail, and it can be very difficult to figure out what the cause is. For this reason, I recommend always closing streams when you're done with them, in a finally block if at all possible. Don't rely on garbage collection - it may happen too slowly for you. Religiously closing files as soon as possible can save you a great many headaches later.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!