Jorge
Jorge

Reputation: 674

BlackBerry: Saving file to text file

I was willing to make a simple application that stores data in a text file according to this entry but I am facing a frustrating exception.

This is my code:

private boolean saveFile(String fileName, String fileContent) {
         DataOutputStream os = null;
          FileConnection fconn = null;
        try {
        fconn =   (FileConnection)Connector.open(fileName,Connector.READ_WRITE);
          if (!fconn.exists())
                fconn.create();
        os=fconn.openDataOutputStream();
        String myString=fileContent;
        os.write(myString.getBytes());
        os.close();
        fconn.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Dialog.alert(e.toString());
            return false;
        }
        return true;
    }

private String getFileName() {

        return "file:///SDCard/BlackBerry/documents/text.dat";

    }

This is the exception I get:

 net.rim.device.api.io.file.FileIOException: File system error

The API says the following:

IOException - if the firewall disallows a connection that is not btspp or comm.

which I don't know if might be helpful or not.

I am using BlackBerry JRE 4.6.1 and a BlackBerry 8900 Simulator. Hope you guys can help me out.

Upvotes: 0

Views: 634

Answers (2)

Jorge
Jorge

Reputation: 674

Ok, the answer is tricky. I kept trying with the same code over and over until I started to think that it was a problem related to the simulator so what I did is, before running the application, I removed and inserted the SD card using the Options item from the Blackberry interface menu and that was it. It worked like charm. I guess it is a bug in the simulator.

Upvotes: 1

Eugen Martynov
Eugen Martynov

Reputation: 20130

Check that your simulator has mounted SDCard. If your is autostart you have to wait until system is completely powered up and SDCard is mounted: example

And the final - you have to close streams and file connection at the end of failed operation also.

Upvotes: 1

Related Questions