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
Trusted Contributor
Posts: 109
Registered: ‎11-15-2011
My Device: blackberry 9800
My Carrier: beginner developer

unable to get the output file

hi,

I try to copy file from res folder to sdcard I use this code. But i'm not able to get the out put file. When I debug, I find that it didn't execute the code in this condition . The file is not created on sdcard. So it didn't exist why is not execute the code? what is the problem with my output in the code?

if (!dstConn.exists()) {}
	
		InputStream in = null;
		FileConnection dstConn = null;
		try {
			Class classs;

			classs =  Application.getApplication().getClass();
			in = classs.getResourceAsStream("/file.js");


			String dstFile = "file:///SDCard/web/file.js";
			try {
				dstConn = (FileConnection) Connector.open(dstFile,
						Connector.WRITE);
				if (!dstConn.exists()) {
					dstConn.create();
					OutputStream out = dstConn.openOutputStream();
					// Copy the bits from instream to outstream
					byte[] buf = new byte[1024];
					int len;

					while ((len = in.read(buf)) > 0) {
						out.write(buf, 0, len);
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		finally {// Close the connections
			try {
				if (dstConn != null) {
					dstConn.close();
					dstConn = null;
				}

			} catch (Exception e) {
			}
			try {
				if (in != null) {
					in.close();
					in = null;
				}

			} catch (Exception e) {
			}
		}

 

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

Re: unable to get the output file

When you use the debugger, whihc statements in this source are executed?

 

Have you defined the SD Card to the Simualtor - see the Simulate (windows) menu in the Windows Menu bar at the top of the Simulator

Trusted Contributor
Posts: 109
Registered: ‎11-15-2011
My Device: blackberry 9800
My Carrier: beginner developer

Re: unable to get the output file

yes, i have an SD card. When I delete this condition 

if (!dstConn.exists()) 

 I can get the output but empty. And I don't know why it didn't execute this block 

	dstConn.create();
					OutputStream out = dstConn.openOutputStream();
					// Copy the bits from instream to outstream
					byte[] buf = new byte[1024];
					int len;

					while ((len = in.read(buf)) > 0) {
						out.write(buf, 0, len);
					}

 if there is the condition

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: unable to get the output file

Did it throw an Exception?

 

You logic looks faulty, if you only go into the if statement when the file does not exist, then an existing file will not get overwritten will it?