Read / write to a file in J2ME without constantly harassing the user

I am writing a simple application for a J2ME phone, and I want to keep the application status 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 

etc. It all seems to work, but it has the following two drawbacks. On my S40 phone every time I launch the application, they ask me: "Allow the application (blah) to write to the file?" or something similar. I have other applications that can save their states (for example, games that save a high-ranking table), and which do not ask me every time about whether they can write to a file. What trick am I missing?

And while I'm here, the file name "///E:/mynewfile.txt" is also not ideal, because it works for my phone, but does not work for my son-phone (and why is this?), That is, I need edit and recompile the application every time I want the program to run on a new phone (I can provide some kind of kludge where the program installs on which phone the application works - there will simply be some of us using it - and then install the line, pointing to a valid file in a valid directory with tvetstvenno, but it's certainly not how it should be done ...). Presumably, I shouldn't write E: / anyway, but is there some kind of canonical "place where application X places all its data files"? And it somehow does not depend on the device, at least to some extent? Again, apparently, I miss the trick - and maybe the two problems I'm asking are related.

What should I do?

+4
source share
2 answers

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

+2
source

1 - You can use "RMS" instead of "fileconnection" to maintain the status of your application, and it does not have harassment.
2-Application opens a connection using Connector.open (), as you do. The input line must contain the full, absolute path to the form:

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

The host element may be empty - and often will, when a line refers to a file on the local host. The root directory corresponds to a logical mount point for a specific storage unit. Root directory names refer to devices . The following table provides 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 the 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 instance:

 String galleryDir = System.getProperty("fileconn.dir.photos"); FileConnection filecon = (FileConnection) Connector.open(galleryDir); 
+6
source

Source: https://habr.com/ru/post/1393445/


All Articles