J2me中读写数据

发布于 2024-12-23 11:10:34 字数 1051 浏览 3 评论 0原文

我正在编写一个将用户数据存储到文件中的应用程序。但是,当我尝试打开手机内存时,我的应用程序会引发安全异常,并且不允许我写入或读取数据。
这是我的代码。

 try
      {
              FileConnection fc = (FileConnection)Connector.open("file:///C:/myfile.dat",Connector.READ_WRITE);
              // If no exception is thrown, the URI is valid but the folder may not exist.
             if (!fc.exists())
             {
                 fc.create();  // create the folder if it doesn't exist
             }
      OutputStream   os=fc.openOutputStream();
      String s="hello how r u..";
      byte[] b=s.getBytes();

      os.write(b);
      os.flush();
             fc.close();
      }
      catch(Exception error )
       {

         Alert alert = new Alert(error.getMessage(), error.toString(), null, null);
         alert.setTimeout(Alert.FOREVER);
         alert.setType(AlertType.ERROR);
         display.setCurrent(alert);

       }

不过我使用 SDycard 来保存数据,效果很好。但是,当我尝试访问手机内存时,有什么解决方案可以逃避 SecurityException 吗?当我将数据存储在 SDCARD 中时,每次都会提示一条消息,要求用户允许应用程序读取或写入数据。我也不想要这个提示信息。
如何摆脱这种情况呢?

I am writing one application which store user data into file. However, when I try to open phone memory my application raise security exception and won't allow me to write or read data.
Here is my code.

 try
      {
              FileConnection fc = (FileConnection)Connector.open("file:///C:/myfile.dat",Connector.READ_WRITE);
              // If no exception is thrown, the URI is valid but the folder may not exist.
             if (!fc.exists())
             {
                 fc.create();  // create the folder if it doesn't exist
             }
      OutputStream   os=fc.openOutputStream();
      String s="hello how r u..";
      byte[] b=s.getBytes();

      os.write(b);
      os.flush();
             fc.close();
      }
      catch(Exception error )
       {

         Alert alert = new Alert(error.getMessage(), error.toString(), null, null);
         alert.setTimeout(Alert.FOREVER);
         alert.setType(AlertType.ERROR);
         display.setCurrent(alert);

       }

However I used SDycard to save data and it works fine. But is there any solution to escape from SecurityException when I try to access phone memory? And when I store data in SDCARD every time one message is prompting that ask user to allow application to read or write data. I also don't want this prompt message.
How to escape from this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感情废物 2024-12-30 11:10:34

您必须签署并证明您的 J2ME 应用程序。这将涉及购买证书。我还没有这样做,所以你必须确认这一点或等待 SO 中的另一个答案。但我非常确定,除非您签署了 midlet,否则手机的安全策略将阻止这种情况发生。

关于如何签署您的 midlet 的一个 URL:

http://m-shaheen.blogspot.com /2009/07/1.html

You will have to sign and certify your J2ME application. This would involve purchasing a certificate. I havent done this, so you would have to confirm this or wait for another answer in SO. But I am pretty sure that unless you sign your midlet the phone's security policy will prevent this.

One URL on how sign your midlet:

http://m-shaheen.blogspot.com/2009/07/1.html

风筝在阴天搁浅。 2024-12-30 11:10:34

同意@Sethu 的观点,OP 是由于 midlet 签名造成的。

OP 分为几个逻辑阶段来解决问题:

  1. 问题/原因:
    每当 midlet 访问受限 API(在本例中为 JSR 75 api)时,它都需要验证其真实性的权限,这有助于防止恶意代码。在您的情况下,midlet 未签名(在 #2 如何签名中进行了解释),因此它没有必要的权限,因此应用程序管理系统会提示用户同意您的 midlet 的每个此类敏感操作。请阅读此链接了解更多详细信息。

  2. 分辨率
    这是一个多步骤的过程,(a) 获取证书,请参阅此链接, (b) 添加必要的权限(对于读取:javax.microedition.io.Connector.file.read,对于写入: JAD 文件中 MIDlet-Permissions 下的 javax.microedition.io.Connector.file.write)

  3. 证书的获取
    此链接给出了详细的解释Java ME 傻瓜签名

Agree with @Sethu that OP is due to midlet signing.

The OP is divided into logical phases to address the issue:

  1. Issue/Cause:
    Whenever a restricted API (in this case JSR 75 api) is accessed by the midlet it needs the permission to validate its authenticity, this helps to keep away malicious code. In your case the midlet is not signed (explained in #2 how to sign) so it does not have the necessary permissions hence Application Management System is prompting for user consent for each such sensitive operation by your midlet. Read this link for more details.

  2. Resolution
    Its a multi step process, (a) Procure the certificate, refer this link, (b) Add the necessary permissions (for read: javax.microedition.io.Connector.file.read, for write: javax.microedition.io.Connector.file.write) under MIDlet-Permissions in the JAD file

  3. Procurement of certificate
    A detailed explaining is given in this link Java ME signing for dummies

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文