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
Posts: 28
Registered: ‎04-08-2010
My Device: 9300
My Carrier: developer

Create file

Hello, I want to create a file (txt) in my blackberry (8310). The error gives me is "File system error 1003"

In the simulator doesn´t work either.

 

 String URL = "file:///SDCard/";
         String filename = "any.txt";
         String data = "Hello World";
         FileConnection conn;
         String mensaje;
         try {
                        conn = (FileConnection) Connector.open( URL+ filename, Connector.READ_WRITE );
                         // If no exception is thrown, then the URI is valid, but the file may or may not exist.
                         if (!conn.exists())
                         {    
                             conn.create();  // create the file if it doesn't exist      
                         }
                         
                         {
                            // create the file
                            OutputStream out = conn.openOutputStream();
                            byte[] b = data.trim().getBytes();
                            String sb = new String(b);
                            System.out.println("Bytes to String: " + sb);
                           
                            if(sb.length() == 0 )
                                System.out.println("String length is zero");
                            else
                                System.out.println("String length is not zero" + sb.length());
                           
                           
                            if(conn.canWrite())
                                out.write(b,0,sb.length());
                            else
                                return "Cannot write to this file";
                            // now write data to the file
                            mensaje="File Size is : "+ conn.fileSize();
                            out.flush();
                            out.close();
                         }

                        conn.close();
                        return mensaje;
                    }
                    catch( IllegalModeException e)
                    {
                        return "IllegalModeException caught in createFile method";

                    }
                    catch( IOException e ){
                        return e.getMessage();
                    }
                    catch( SecurityException e ){
                        // no permission to create/write
                        return "SecurityException caught in createFile method";
                    }
        }

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

Re: Create file

[ Edited ]

From memory File system error 1003 usually means there is no SD Card present.  Do you have one on your Phone?

Contributor
Posts: 28
Registered: ‎04-08-2010
My Device: 9300
My Carrier: developer

Re: Create file

I´m not sure, how can i know where i can save my files? I don´t know which is the system files of a blackberry.

I have tested with a file in an external server, but I have the exception ClassCastException in the FileConnection.

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

Re: Create file

"how can i know where i can save my files?"

 

Not sure what you are asking here.  But I think this might help:

How To – Programmatically determine if a microSD card has been inserted
Article Number: DB-00560
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/1295814/How_To...

 

" have tested with a file in an external server, but I have the exception ClassCastException in the FileConnection"

Sorry, I don't know what you mean by this. 

 

When you debugged this, what the class that you had got?  What URI did you use to access this file?  You can't use FileConnection to access files directly off the Web. 

Contributor
Posts: 28
Registered: ‎04-08-2010
My Device: 9300
My Carrier: developer

Re: Create file

Thanks, it works with "file:///store/home/user/documents/file.txt".

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

Re: Create file

Great!  Smiley Happy

 

Can you mark this Thread as solved?