Posted by: robrobertson | January 17, 2012

Copy a File from a BlackBerry Java Project to the Local File System

The key to storing a file in your COD for deployment to the BlackBerry file system:  store it in the src folder. Then use the basic input and output streams for reading and writing.

/**
* Write out a file stored in the COD.
* @param filename - filename must start with /
* @param location - example: file:///store/home/user
* @throws Exception
*/
private void writeFile(String filename, String location) throws Exception
{
InputStream inputStream = null;
inputStream = getClass().getResourceAsStream(filename);

if(inputStream == null)
    throw new Exception("Unable to open input file: " + filename);

FileConnection fc = (FileConnection)Connector.open(location + filename);
if (!fc.exists()) {
    fc.create();
}
OutputStream outStream = fc.openOutputStream();
outStream.write(IOUtilities.streamToBytes(inputStream));
outStream.close();
fc.close();
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.