|
-
Feb 4th, 2005, 11:36 PM
#1
Thread Starter
Addicted Member
[File].delete();
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????!!!!! 
<Moderator added a green checkmark to the first post, to show that it is resolved>
Last edited by NoteMe; Feb 17th, 2005 at 06:58 AM.
To protect time is to protect everything...
-
Feb 5th, 2005, 08:08 AM
#2
Frenzied Member
Re: [File].delete();
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)
-
Feb 5th, 2005, 08:09 AM
#3
Frenzied Member
Re: [File].delete();
Here is something I did, that deletes the cookies,
Code:
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");
}
}
}
-
Feb 5th, 2005, 02:27 PM
#4
Thread Starter
Addicted Member
Re: [File].delete();
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
To protect time is to protect everything...
-
Feb 5th, 2005, 02:38 PM
#5
Frenzied Member
Re: [File].delete();
Here is how to SIMPLY delete a file:
Code:
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?
-
Feb 5th, 2005, 02:45 PM
#6
Dazed Member
Re: [File].delete();
He probably is required to catch an IOException or specifically a FileNotFoundException.
-
Feb 5th, 2005, 02:52 PM
#7
Thread Starter
Addicted Member
Re: [File].delete();
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?
To protect time is to protect everything...
-
Feb 5th, 2005, 02:57 PM
#8
Frenzied Member
Re: [File].delete();
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.
-
Feb 5th, 2005, 02:58 PM
#9
Frenzied Member
Re: [File].delete();
 Originally Posted by Virtual24
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?
-
Feb 5th, 2005, 03:14 PM
#10
Thread Starter
Addicted Member
Re: [File].delete();
Code:
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.
To protect time is to protect everything...
-
Feb 5th, 2005, 03:20 PM
#11
Thread Starter
Addicted Member
Re: [File].delete();
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.
To protect time is to protect everything...
-
Feb 5th, 2005, 04:00 PM
#12
Frenzied Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|