我正在编写一个将用户数据存储到文件中的应用程序。但是,当我尝试打开手机内存时,我的应用程序会引发安全异常,并且不允许我写入或读取数据。
这是我的代码。
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 中时,每次都会提示一条消息,要求用户允许应用程序读取或写入数据。我也不想要这个提示信息。
如何摆脱这种情况?
最佳答案
您必须签署并证明您的 J2ME 应用程序。这将涉及购买证书。我还没有这样做,所以你必须确认这一点或等待 SO 中的另一个答案。但我非常确定,除非您签署了 midlet,否则手机的安全策略将阻止这种情况发生。
关于如何签署您的 midlet 的一个 URL:
关于java - J2me中读写数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677383/