【问题标题】:BlackBerry: Saving file to text file黑莓:将文件保存到文本文件
【发布时间】:2012-01-11 22:31:43
【问题描述】:

我愿意根据entry 制作一个将数据存储在文本文件中的简单应用程序,但我面临一个令人沮丧的异常。

这是我的代码:

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";

    }

这是我得到的例外:

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

API 说明如下:

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

不知道有没有用。

我正在使用 BlackBerry JRE 4.6.1 和 BlackBerry 8900 Simulator。 希望大家帮帮我。

【问题讨论】:

  • 哪一行抛出异常?写?
  • 你在调试的时候发现fileName的值是多少?
  • 您的要求是保存文本文件,但在 getFileName() 方法中您提供的是“filename.dat”。对于文本文件,它应该是“fileName.txt”。你得到了什么例外?
  • 可以查看FileIOException.getErrorCode()返回的值。
  • @Tamar fconn = (FileConnection)Connector.open(fileName,Connector.READ_WRITE); 正在抛出异常

标签: java blackberry blackberry-simulator


【解决方案1】:

检查您的模拟器是否已安装 SDCard。 如果您是自动启动,则必须等到系统完全启动并安装 SDCard:example

最后 - 您还必须在失败操作结束时关闭流和文件连接。

【讨论】:

    【解决方案2】:

    好的,答案很棘手。我一遍又一遍地尝试使用相同的代码,直到我开始认为这是与模拟器有关的问题,所以我所做的是,在运行应用程序之前,我删除插入 SD 卡使用黑莓界面菜单中的Options 项目,仅此而已。它就像魅力一样。我猜这是模拟器中的一个错误。

    【讨论】:

    • 你的程序是多线程的吗?可能是您试图同时将数据保存在同一个文件中?检查另一个 JRE 或另一个模拟器。我会将模拟器问题归咎于可能性列表中的最后一个选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2012-09-27
    • 2021-12-13
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多