0

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.

Community
  • 1
  • 1
Jorge
  • 664
  • 1
  • 12
  • 33
  • which line is throwing the exception? the write? – Tamar Jan 11 '12 at 22:51
  • What is the value of `fileName` you found while debugging? – Rupak Jan 12 '12 at 06:09
  • Your requirement is save the text file but in the getFileName() method you are giving "filename.dat". For text file it should be "fileName.txt". and what exception you are getting? – alishaik786 Jan 12 '12 at 06:19
  • You can check the value returned by `FileIOException.getErrorCode()`. – Rupak Jan 12 '12 at 06:22
  • @Tamar `fconn = (FileConnection)Connector.open(fileName,Connector.READ_WRITE);` is throwing the exception – Jorge Jan 12 '12 at 13:03
  • @Rupak `file:///SDCard/BlackBerry/documents/text.dat` is the value of `fileName`. This file does not exist. I want to create this new file with the contents of `fileContent` – Jorge Jan 12 '12 at 13:05
  • @Rupak The error code is 1003. – Jorge Jan 12 '12 at 13:11
  • I managed to make it work reinserting the SD card. Thanks anyway. – Jorge Jan 12 '12 at 13:54
  • @George, well done. And 1003 stands for `FileIOException.NO_SUCH_ROOT`, from http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/io/file/FileIOException.html – Rupak Jan 12 '12 at 16:19

2 Answers2

1

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.

Community
  • 1
  • 1
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
1

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.

Jorge
  • 664
  • 1
  • 12
  • 33
  • Is your program multithreading? Could be that you are trying to save data in the same file at the same time? Check another JRE or another simulator. I would blame simulator issue as last option from list of possibilities. – Eugen Martynov Jan 12 '12 at 19:33