Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 3
Registered: ‎02-11-2012
My Device: BlackBerry Curve 8520
My Carrier: Personal
Accepted Solution

How to delete files?

Hi BlackBerry Support Community.

I have a question.

Well, i want to delete a file from the SDCard from my BlackBerry using Java.

I have no idea how to do.

Can you help me?

Developer
Posts: 1,636
Registered: ‎07-14-2008
My Device: Z10
My Carrier: Fido

Re: How to delete files?

do something like this

 

import javax.microedition.io.file.*;


 

           try

           {

                    FileConnection fileConnection = (FileConnection)Connector.open("file:///SDCard/BlackBerry/file_to_delete.txt", Connector.READ_WRITE);            

                if (fileConnection.exists())
                    fileConnection.delete();

               fileConnection.close();

            }
            catch(Exception ioe)
            {
            }



New Contributor
Posts: 3
Registered: ‎02-11-2012
My Device: BlackBerry Curve 8520
My Carrier: Personal

Re: How to delete files?

[ Edited ]

Thanks!!!, works!
Greetings!

One Question more,

how do I get to remove all files in a directory?, i use this:

("file:///SDCard/dir/*.*")

But this...not found...


New Developer
Posts: 12
Registered: ‎11-28-2011
My Device: 9800
My Carrier: O2

Re: How to delete files?

Get all file names and loop
New Contributor
Posts: 3
Registered: ‎02-11-2012
My Device: BlackBerry Curve 8520
My Carrier: Personal

Re: How to delete files?

But...is for delete temps...i don't know the names of the files...

Other solution?

Highlighted
New Developer
Posts: 12
Registered: ‎11-28-2011
My Device: 9800
My Carrier: O2

Re: How to delete files?

Try something like this...
        try{
            FileConnection fc = (FileConnection) Connector.open("file:///SDCard/BlackBerry/path/");
            Enumeration filelist = fc.list("*" , true);
            while(filelist.hasMoreElements()) {
                String fileName = (String) filelist.nextElement();
                FileConnection filenames = (FileConnection) Connector.open("file:///SDCard/BlackBerry/path/"+ fileName ,Connector.READ_WRITE);
                if(filenames.exists()){
                    filenames.delete();
                    filenames.close();                
                }
            }
			fc.close(); 
        }catch (Throwable t){
            t.printStackTrace(); 
            String errorString = t.toString();
            // content = "ERROR\n" + errorString;   
        }