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
Contributor
vikbadu
Posts: 13
Registered: ‎04-14-2011
My Device: playbook,SmartPhones
Accepted Solution

Cannot copy database from res folder and write into memory card

I am developing a small application using blackberry jde 7.0. I want to copy the database(.db) from res folder and write into the memory card.When i run in the simulator it throws exception  'FileNotFound'. But the file is in the res folder.

Help me guys....

 

Following is the code

 

 copyFile("/Demo.db","file:///SDCard/Databases/Demo.db");     

 

 

//Copy to Memory card
public void copyFile(String srFile, String dtFile)
{
try
{ 
FileConnection fconn; 
fconn = (FileConnection) Connector.open(dtFile,Connector.READ_WRITE);
//if(!fconn.exists()) // if file does not exists , create a new one
// {
// fconn.create();
// }
InputStream is = (InputStream)getClass().getResourceAsStream(srFile);
System.out.println("read" + String.valueOf(is.available()));
OutputStream os =fconn.openOutputStream();
byte[] buf = new byte[is.available()];
int len;
while ((len = is.read(buf)) > 0)
{
os.write(buf, 0, len);
}
is.close();
os.close();
}
catch(IOException e)
{
System.out.println("Exception"+e.getMessage());
}
}

 The exception is

 

File not found

 

Developer
pradeep_ch
Posts: 543
Registered: ‎12-23-2010
My Device: Z10

Re: Cannot copy database from res folder and write into memory card

I think the problem with the destination directory.

 

try copyFile() funtiom like this.

 

private void copyDatabase(String src, String dst) {
		try
		{ 
		FileConnection fconn; 
		fconn = (FileConnection) Connector.open(dst,Connector.READ_WRITE);
		if (!fconn.exists())
                        fconn.mkdir();
		fconn.close();
		fconn = null;
		fconn = (FileConnection) Connector.open(dst+"Demo.db",Connector.READ_WRITE);
		if (!fconn.exists())
            fconn.create();
		//if(!fconn.exists()) // if file does not exists , create a new one
		// {
		// fconn.create();
		// }
		InputStream is = (InputStream)getClass().getResourceAsStream(src);
		System.out.println("read" + String.valueOf(is.available()));
		OutputStream os =fconn.openOutputStream();
		byte[] buf = new byte[is.available()];
		int len;
		while ((len = is.read(buf)) > 0)
		{
		os.write(buf, 0, len);
		}
		is.close();
		os.close();
		}
		catch(IOException e)
		{
		System.out.println("Exception"+e.getMessage());
		}
	}

 Thanks.

Developer
pradeep_ch
Posts: 543
Registered: ‎12-23-2010
My Device: Z10

Re: Cannot copy database from res folder and write into memory card

and the calling of copyFile function will be..

 

copyDatabase("/Demo.db","file:///SDCard/Databases/");

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Cannot copy database from res folder and write into memory card

"When i run in the simulator it throws exception  'FileNotFound'."

What line throws this?

 

You might find the code in the SQLiteDemo sample application, which does pretty much the same thing, useful to study to determine what has gone wrong. 

Contributor
vikbadu
Posts: 13
Registered: ‎04-14-2011
My Device: playbook,SmartPhones

Re: Cannot copy database from res folder and write into memory card

The problem is i didnt create the destination file in the Databases folder before write there. Now the problem is resolved. Thank u very much.