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
Developer
nikita18
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520

FileConnection.delete() not working on device

Hello Friends, I am using the code below to delete files but its not working. Can anyone tell me the reason?

 

private Vector undeletedFiles=new Vector();	

public void delete(String path)
    {
		FileConnection fileConn=null;
        try
        {
            fileConn = (FileConnection)Connector.open(path, Connector.READ_WRITE);
            if(fileConn.exists()){
            	
	            if(fileConn.isDirectory())                     
	            	
	            	deleteFolder(path);	   
	            	 
	            else{
	            	
	                fileConn.delete();	               
	                
	                fileConn.close();
	                
	                System.out.println("File Deleted: "+path);
	            }
            }
        }catch(IOException ioe) {
        	if(fileConn.isOpen())
				try {
					fileConn.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	System.out.println("1 Exception : " + ioe.toString() +", "+ioe.getMessage()+"\nPath: "+path);
            undeletedFiles.addElement(path);
            System.out.println("File Added to undeleted list. "+path);
           	
        }
    }
	
	public void deleteFolder(String fullPath) {
		FileConnection dirConn=null;
        try {
            dirConn = (FileConnection)Connector.open(fullPath, Connector.READ_WRITE);
            if(dirConn.exists() && dirConn.isDirectory()) {
                for(Enumeration e = dirConn.list("*", true); e.hasMoreElements();) {
                    String name = e.nextElement().toString();
                    delete(fullPath + name);
                }
                dirConn.delete();
            }
            dirConn.close();
            System.out.println("Directory Deleted: "+fullPath);
        } catch(IOException ioe) {
        	if(dirConn.isOpen())
				try {
					dirConn.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
           
        	System.out.println("2 Exception : " + ioe.toString()+", Path:"+fullPath);
          
            try {            	
				FileConnection fileCon = (FileConnection)Connector.open(fullPath,Connector.READ_WRITE);				
				if((fileCon.getURL().equals("file:///SDCard/"))){
					fileCon.close();
					return;
				}
				else{
					 undeletedFiles.addElement(fullPath);
					 System.out.println("File Added to undeleted list. "+fullPath);
				}
            } catch(IOException e){
            	 System.out.println("IOException: "+e.getMessage()+", path:"+fullPath);	
            	 undeletedFiles.addElement(fullPath);
            }  
		}
    }
    

 

 

I am calling this code using: delete("file:///SDCard/"); 

 

 

Please use plain text.
New Contributor
javabuddy
Posts: 5
Registered: ‎01-15-2011
My Device: BOLD

Re: FileConnection.delete() not working on device

Please use plain text.
Developer
nikita18
Posts: 144
Registered: ‎08-18-2012
My Device: Blackberry Curve 8520

Re: FileConnection.delete() not working on device

Blackberry Curve 8520
Please use plain text.