简体   繁体   English

BlackBerry:将文件保存到文本文件

[英]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: API说明如下:

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. 我正在使用BlackBerry JRE 4.6.1和BlackBerry 8900 Simulator。 Hope you guys can help me out. 希望你们能帮助我。

Check that your simulator has mounted SDCard. 检查您的模拟器是否已安装SDCard。 If your is autostart you have to wait until system is completely powered up and SDCard is mounted: example 如果你是自动启动,你必须等到系统完全上电并安装SDCard: 例如

And the final - you have to close streams and file connection at the end of failed operation also. 最后 - 你还必须在失败的操作结束时关闭流和文件连接。

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. 我一直在尝试使用相同的代码,直到我开始认为这是与模拟器相关的问题所以我所做的是,在运行应用程序之前,我使用Blackberry界面中的Options删除插入 SD卡菜单就是这样。 It worked like charm. 它像魅力一样工作。 I guess it is a bug in the simulator. 我想这是模拟器中的一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM