在J2ME中读取/写入文件而不会不断纠缠用户

Kev*_*ard 4 io midp java-me series-40

我正在编写一个简单的J2ME手机应用程序,我想在退出时保存应用程序的状态.
谷歌搜索引导我到FileConnection类:

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

等等.这一切似乎都有效,它有以下两个缺点.在我的S40手机上,每次运行应用程序时,都会被问到"让应用程序(等等)写入文件?" 或某些这样的事情.我有其他可以保存状态的应用程序(例如保存高分表的游戏),并且每次都不会问我是否可以写入文件.我错过了什么诀窍?

虽然我在这里 - "///E:/​​mynewfile.txt"文件名也不理想,因为它适用于我的手机,但不能用于我儿子的手机(为什么要这样?) ,这意味着每次我希望程序在新手机上运行时我都要编辑和重新编译应用程序(我可以设想一些程序确定应用程序正在运行的手机的某种程度 - 只会有一些我们使用它 - 然后相应地设置一个指向有效目录中的有效文件的字符串,但这肯定不是它应该如何完成...).大概我不应该写E:/无论如何,但是有某种规范的"应用程序X放置所有数据文件的地方"吗?它是否与设备无关,至少在某种程度上如此?再一次,大概我错过了一个伎俩 - 我要问的两个问题可能是相关的.

我该怎么办?

has*_*ian 6

1 - 您可以使用"RMS"代替"fileconnection"来保存您的应用程序状态,它没有任何纠缠.
2 - 应用程序使用Connector.open()打开连接.输入字符串必须包含表单的完全限定的绝对路径名:

file://<host>/<root>/<directory>/<directory>/.../<name>
Run Code Online (Sandbox Code Playgroud)

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

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:////");   
Run Code Online (Sandbox Code Playgroud)

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

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   
Run Code Online (Sandbox Code Playgroud)

例如:

String galleryDir = System.getProperty("fileconn.dir.photos");   
FileConnection filecon = (FileConnection) Connector.open(galleryDir);   
Run Code Online (Sandbox Code Playgroud)


Kev*_*ard 2

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