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
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

File Creation Problems.

conn = (HttpConnection) Connector.open(URL);
inputStream = conn.openDataInputStream();
document = builder.parse( inputStream );
rootElement = document.getDocumentElement();
rootElement.normalize();
displayNode(rootElement);

fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/documents/" + URL.substring(URL.lastIndexOf('/')+1)+ ".xml" ,Connector.READ_WRITE);
if(!fc.exists())
fc.create();
fc.setWritable(true);
os =fc.openOutputStream();

byte[] buffer = new byte[200];
while (true)
{
int bytesRead = inputStream.read(buffer, 0, 200);
if (bytesRead == -1)
break;
os.write(buffer, 0, bytesRead);
}

 

The above is my code to save a file on to disc. The problem is the file is creating with the desired name at the correct location. Ooops!!!!!! But there is no data in it. It is a empty file of size 0kb. where is the problem?? is there any thing wrong in the code?

 

I placed this code in the public void run() {} of the Thread class. And i have nearly 20 threads to start at the start of my app. So, all these threads will access them. Is this the problem with the threads?? I didnt understand where am wrong...!!!

 

 

 

Thanks and Regards,

PraveenGoparaju.

Thanks and Regards,
PraveenGoparaju.
Developer
BBDeveloper
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: File Creation Problems.

There is nothing wrong in the code.. what is the content you trying to write. Have you checked what exception is generated?

Use Search. "Accept Solution" If the problem is resolved.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: File Creation Problems.

[ Edited ]

Thanks for your reply,

 

 

It is not giving any exception. But there is no data when i see in the file. the file is creating at the specified location. the app is running fine. Actually am trying to parse the inputstream before saving to disc. is it causing any problem??

 

this is my actual code snippet.

try{ factory = DocumentBuilderFactory.newInstance(); builder = factory.newDocumentBuilder(); conn = (HttpConnection) Connector.open(URL); inputStream = conn.openDataInputStream(); document = builder.parse( inputStream ); rootElement = document.getDocumentElement(); rootElement.normalize(); displayNode(rootElement); synchronized(this){ fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/documents/" + URL.substring(URL.lastIndexOf('/')+1)+ ".xml" ,Connector.READ_WRITE); if(!fc.exists()) fc.create(); fc.setWritable(true); os =fc.openOutputStream(); inputStream.reset(); byte[] buffer = new byte[200]; while (true) { int bytesRead = inputStream.read(buffer, 0, 200); if (bytesRead == -1) break; os.write(buffer, 0, bytesRead); } } }

 

 

 

 so please tell me where am wrong. the file is creating, but the data is not saving....!!!!!! thats the problem. 

 

 

Thanks and Regards,

PraveenGoparaju

 

 

Message Edited by Blanc on 09-17-2009 10:47 AM
Thanks and Regards,
PraveenGoparaju.
BlackBerry Development Advisor
MSohm
Posts: 14,752
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: File Creation Problems.

Have you tried to print out your data to the debug console (System.out.println)?  Please esure that you are properly closing your connections.

 

How To - Close connections
Article Number: DB-00530

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/How_To_...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: File Creation Problems.

Thanks MSohm,

 

I closed the connections properly and that to in the finally block only. I go through the links you have given. So, at any time we can make 5 TCPconnections open?? please clarify this.??

 

Am simultaneously opening around 15 HTTP connections using Threads, is this creating any problems?? If so, please tell me how can i manage these so that i can over come the problem.

 

I didnt tried with System.out.println, but when i opened with 1 HttpConnection (this time not 15 connections )  i found that the data is writing successfully. With 15connections also Files are Creating, but there is no data in them.

 

Thanks and Regards,

PraveenGoparaju 

Thanks and Regards,
PraveenGoparaju.
Developer
Guest
Posts: 146
Registered: ‎11-05-2008
My Device: Not Specified

Re: File Creation Problems.

Have you used flush() method ?
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: File Creation Problems.

Hi, No flush() method
Thanks and Regards,
PraveenGoparaju.
Developer
editormax
Posts: 133
Registered: ‎07-20-2009
My Device: Not Specified

Re: File Creation Problems.

Did you close your connection?

 

os.close();

conn.close();