reading from/writing to a file in J2ME without continually pestering the user
Asked Answered
H

2

5

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?

Halidom answered 27/1, 2012 at 22:0 Comment(6)
It is due to certificate related signing ! You must buy certificate and these questions will disappear.Demi
What? I have to pay for the privilege of being able to write a device-independent app?? Meh :-/Halidom
That's the reality. To be able to remove most, if not all, notifications, you need to buy a certificate.Rodas
There is some big misunderstanding here. I have figured out the answer to my question and it doesn't involve paying anyone anything. The answer is to use the methods of the RecordStore class to read and write to a file which is placed in the resources of the program. This seems to work fine for me -- in the sense that I've tried my program on two phones and it saves and loads data fine, between sessions, without ever pestering the user.Halidom
An example of all this in action is at developer.nokia.com/Community/Wiki/… .Halidom
@KevinBuzzard: I think the misunderstanding was, the above commenters thought you were asking how to write to a "normal" file, managed by your code, as shown in your example code... as opposed to a resource store managed by the system, which doesn't necessarily correspond to a single file, and which doesn't necessarily write to files at the time when you store a value.Invincible
H
2

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.

Halidom answered 27/1, 2012 at 22:1 Comment(0)
B
7

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);   
Bufordbug answered 25/2, 2012 at 11:45 Comment(0)
H
2

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.

Halidom answered 27/1, 2012 at 22:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.