4

我正在编写一个简单的 J2ME 电话应用程序,我想在退出时保存应用程序的状态。
谷歌搜索将我带到 FileConnection 类:

FileConnection filecon = (FileConnection) Connector.open("file:///E:/mynewfile.txt");
filecon.create();
// write data to file etc etc

之类的。这一切似乎都有效,它有以下两个缺点。在我的 S40 手机上,每次运行应用程序时,都会询问我“让应用程序(等等)写入文件?” 或类似的东西。我还有其他可以保存状态的应用程序(例如保存高分表的游戏),并且不会每次都询问我是否可以写入文件。我缺少什么技巧?

虽然我在这里——“///E:/​​mynewfile.txt”文件名也不理想,因为它适用于我的手机,但不适用于我儿子的手机(为什么要这样?) ,这意味着每次我希望程序在新手机上运行时,我都必须编辑和重新编译应用程序(我可以设想某种程序确定应用程序在哪部手机上运行的组合——只有少数几个我们使用它——然后相应地设置一个指向有效目录中有效文件的字符串,但这肯定不是它应该如何完成的......)。大概我不应该写 E:/ ,但是是否有某种规范的“应用程序 X 放置其所有数据文件的地方”?至少在某种程度上它是否与设备无关?再一次,大概是我

我应该做什么?

4

2 回答 2

6

1-您可以使用“RMS”而不是“fileconnection”来保存您的应用程序状态,它没有纠缠。
2-应用程序在您执行此操作时使用 Connector.open() 打开连接。输入字符串必须包含以下形式的完全限定的绝对路径名:

file://<host>/<root>/<directory>/<directory>/.../<name>

主机元素可能是空的 - 当字符串引用本地主机上的文件时,通常会是空的。根目录对应于特定存储单元的逻辑安装点。根名称是特定于设备的。下表提供了一些根值示例以及如何打开它们:

CFCard/   
FileConnection fc = (FileConnection) Connector.open("file:///CFCard/");   
SDCard/   
FileConnection fc = (FileConnection) Connector.open("file:///SDCard/");   
MemoryStick/   
FileConnection fc = (FileConnection) Connector.open("file:///MemoryStick/");   
C:/   
FileConnection fc = (FileConnection) Connector.open("file:///C:/");   
/   
FileConnection fc = (FileConnection) Connector.open("file:////");   

System.getProperty() 方法必须获得一些特殊的根:

fileconn.dir.photos: Image capture through your Mobile camera.  
fileconn.dir.videos: Vedio capture through your Mobile camera.  
fileconn.dir.tones: Ring tones default directory.   
fileconn.dir.memorycard: Memory Card , SD Card , Flash Drive root directory   
fileconn.dir.private: Working directory of midlet   

例如:

String galleryDir = System.getProperty("fileconn.dir.photos");   
FileConnection filecon = (FileConnection) Connector.open(galleryDir);   
于 2012-02-25T11:45:36.620 回答
2

我自己对我的问题的回答:我可以使用 RecordStore 类的方法来读取和写入放置在程序资源中的文件。

于 2012-07-15T12:39:01.113 回答