PDA

Click to See Complete Forum and Search --> : [File].delete();


Virtual24
Feb 4th, 2005, 10:36 PM
My problem is simple. I have a file object, It IS valid, the file DOES exist. It is NOT a directory. I want to delete it so I call .delete(); on the object. It throws NO exception, but returns false, and DOESN'T DELETE IT. WHY????!!!!! :confused:





<Moderator added a green checkmark to the first post, to show that it is resolved>

System_Error
Feb 5th, 2005, 07:08 AM
why don't you post your code and let us take a look at what you have..

In the mean time, make sure you did all of these:

Did you specify the ENTIRE path of the file and not just the directory, I did this once and it took me forever to figure out that all I needed was the filename at the end

Make sure you CAN delete it. Although I think you can delete the files you don't even have permision to modify(I accidently did this at school, terrible situation)

System_Error
Feb 5th, 2005, 07:09 AM
Here is something I did, that deletes the cookies,


import java.io.*;

class Delete
{
int count;
File[] files;
public void nuke(File path) throws IOException
{
files = path.listFiles();

for(int i=0; i<files.length; ++i)
{
count++;
if(files[i].isDirectory())
nuke(files[i]);

files[i].delete();
}
}
public int getCount()
{
return count;
}
public static void main(String[] args) throws IOException
{
Delete d = new Delete();
File f = new File("C:/Documents and Settings/Sheppard/Cookies");
System.out.println("Would you like to delete all the cookies?");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
if (str.equals("yes") || str.equals("Yes"))
{
d.nuke(f);
System.out.println("There were " + d.getCount() + " files found" + "\n");
System.out.println("The following files were deleted successfully:");
for (int i=0; i<d.files.length; i++)
{
System.out.println(d.files[i].toString());

}
}
else
{
System.out.println("Cookies will not be deleted");

}
}
}

Virtual24
Feb 5th, 2005, 01:27 PM
The code is a bit complex to paste the whole thing but basically I have a File object that represents a file called 'data.obj' I have the permission to delete the file (I would assume) because the file was made by the same program. I've printed out the path to System.out to make sure its right, and it is. When I do a .delete() on it though, it simply returns false (no exceptions). I'm so confused... lol :confused: :confused: :confused:

System_Error
Feb 5th, 2005, 01:38 PM
Here is how to SIMPLY delete a file:


import java.io.*;

public class abc1
{
public static void main(String[] args)
{
File file = new File("C:\\Documents and Settings\\Sheppard\\Desktop\\New Text Document.txt");
file.delete();
}
}


Is this a little bit like the code you have to delete it?

Since the code is too long, do you mind just posting the snippet that deletes the file?

Another thing, what kind of exception are you throwing or catching?

Dillinger4
Feb 5th, 2005, 01:45 PM
He probably is required to catch an IOException or specifically a FileNotFoundException.

Virtual24
Feb 5th, 2005, 01:52 PM
Ok. I know how to simply delete the file. The point is it ISNT working. I figured out that I don't have permission to delete it. The file was created by the same program using an ObjectOutputStream. When I created a new SecurityManager and used it to .checkDelete( ... ); on the file it threw a SecurityException with 'access denied.' I don't understand why I don't have the permission to delete it. How do I get it?

System_Error
Feb 5th, 2005, 01:57 PM
I doubt this could be it, but how were you seperating each directory? Like this:

C:/

or did you write it like this, the correct way, and to avoid an error with escape sequences:

C:\\

I know this may sound stupid, but I did it, and it drove me crazy trying to fix it.

System_Error
Feb 5th, 2005, 01:58 PM
Ok. I know how to simply delete the file. The point is it ISNT working. I figured out that I don't have permission to delete it. The file was created by the same program using an ObjectOutputStream. When I created a new SecurityManager and used it to .checkDelete( ... ); on the file it threw a SecurityException with 'access denied.' I don't understand why I don't have the permission to delete it. How do I get it?

Never mind that last post...

Have you right clicked and checked the properities of the file?

Virtual24
Feb 5th, 2005, 02:14 PM
HashMap<String,String> hm = new HashMap<String,String>();
hm.put( "tes", "testing" );
File f = new File( System.getProperty( "user.dir" ) + "\\res\\data.obj" );
PropertyResource pr = new PropertyResource( f, hm )
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( f ) );
oos.writeObject( pr );
oos.flush();
oos.close();

ResourceManager rm = ResourceManager.getInstance();

Resource r = rm.useResource( rm, "data.obj" );
System.out.println( r.getObject( "test" ) );
rm.releaseResource( rm, r );

rm.deleteResource( r );



public void deleteResource( Resource r )
{
try{

ArrayList assoc = resource_assoc.get( r );
if( assoc != null )
{
throw new ResourceInUseException( r, assoc );
}
r.getResourceFile().delete();

}
catch( Exception e ){ Error.processError( e ); }
}


classes PropertyResource, ResourceManager, and Resource are classes I have made to handle loading files as resources. It simply loads all files in the dir "[user.dir]\res" I know this all works because I can read from the file. It just wont delete.

Virtual24
Feb 5th, 2005, 02:20 PM
OK WOW IM DUMB....... I fixed it. I forgot to close the ObjectInputStream, so the file was showing up as in use to the VM.

System_Error
Feb 5th, 2005, 03:00 PM
it happens!