在 J2ME 中读取/写入文件而不持续困扰用户

发布于 2024-12-29 04:59:47 字数 715 浏览 1 评论 0原文

我正在编写一个简单的 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 放置其所有数据文件的地方”?它是否与设备无关,至少在某种程度上?再说一遍,我可能错过了一个技巧——而且我要问的两个问题可能是相关的。

我应该做什么?

I'm writing a simple J2ME phone app and I want to save the status of the app when I exit it.
Googling around led me to the FileConnection class:

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

and such like. This all seems to work, but it has the following two drawbacks. On my S40 phone, every time I run the app, I am asked "let application (blah) write to a file?" or some such thing. I have other apps that can save their states (e.g. games that save a high score table) and which don't ask me every time about whether they can write to a file. What is the trick I'm missing?

And while I'm here -- the "///E:/mynewfile.txt" file name isn't ideal either, because it works for my phone but doesn't work for my son's phone (and why should it?), meaning I have to edit and recompile the app every time I want the program to run on a new phone (I can envisage some sort of kludge where the program establishes whose phone the app is running on -- there will just be a few of us using it -- and then sets a string pointing to a valid file in a valid directory accordingly, but this is surely not how it's supposed to be done...). Presumably I shouldn't be writing to E:/ anyway, but is there some sort of canonical "place where application X puts all its data files"? And is it somehow device-independent, at least to some extent? Again, presumably I'm missing a trick -- and the two issues I'm asking about are perhaps related.

What should I be doing?

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

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

发布评论

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

评论(2

南薇 2025-01-05 04:59:47

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

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

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

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:////");   

一些特殊的 root 必须通过 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);   

1-You can use "RMS" instead of "fileconnection" to save your application status and it has no pestering.
2-An application opens a connection using Connector.open() as you do it. The input string must comprise a fully qualified, absolute pathname of the form:

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

The host element may be empty - and often will be, when the string refers to a file on the local host. The root directory corresponds to a logical mount point for a particular storage unit. Root names are device-specific. The following table provides some examples of root values and how to open them:

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:////");   

Some special root must be earned by System.getProperty() method:

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   

For example:

String galleryDir = System.getProperty("fileconn.dir.photos");   
FileConnection filecon = (FileConnection) Connector.open(galleryDir);   
南薇 2025-01-05 04:59:47

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

My own answer to my question: I can use the methods of the RecordStore class to read and write to a file which is placed in the resources of the program.

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