File IO in J2ME

Status
Not open for further replies.

RCuber

The Mighty Unkel!!!
Staff member
Hey guys,
How can one store and retrive values from a textfile using J2ME.
textfile will have content something like this

Code:
#--Configuration File--
Value1=Hello
Value2=World

I need to read the values in the following format.
Code:
string V1;
string V2;

V1=GetConfiguration(Value1);
V2=GetConfiguration(Value2);

For writing into the file I need the following format
Code:
SetConfiguration(Value1,"test1");
SetConfiguration(Value2,"test2");

is there a better way of doing it, does J2ME provide application configuration class or something like that?

This is for one of my friend.

EDIT: Or can I use XML to store data? Does J2ME support native XML reading and writing.

@Chandru.in im counting on you ;)
 
Last edited:

chandru.in

In the zone
Ooops!

I'm more of enterprise (Spring and Hibernate) and desktop Java developer than Mobile. :D

File IO is not the right way for storing preferences in Java ME as the FileConnection class may not be implemented by all devices. For storing preferences in Java ME, the RecordStore class is to be used. Ask him to look into its methods for further information, as I cannot get the exact scenario of his requirement.

He has to be careful about opening and close Recordstores as it has to be closed as many times as it is opened. Ask him to use Singleton pattern for managing configurations to avoid such hassles.
 
Last edited:

mastermunj

In the zone
Try this

But it requires JSR-75. If its not available and you do not have to store huge data.. solution suggested by chandru.in is a better option...
 
OP
RCuber

RCuber

The Mighty Unkel!!!
Staff member
File IO is not the right way for storing preferences in Java ME as the FileConnection class may not be implemented by all devices.
ok then File IO is off the drawing board :p.

For storing preferences in Java ME, the RecordStore class is to be used.

Ill as him to take a look at this page.

I cannot get the exact scenario of his requirement.

requirement is simple, read two values from two textboxes and store it somewhere for future use. retrive the two values when every needed.


Try this

But it requires JSR-75. If its not available and you do not have to store huge data.. solution suggested by chandru.in is a better option...
Thanks for the link , This is indeed usefull for him in other cases.

@Chandru: Im am still curious about storing the values in a XML file and reading it :D .. I read somewhere that J2ME does support it.
 

chandru.in

In the zone
Thanks for the link , This is indeed usefull for him in other cases.
JSR-75 is the one providing FileConnection class and many devices do not support it.

@Chandru: Im am still curious about storing the values in a XML file and reading it :D .. I read somewhere that J2ME does support it.
AFAIK Java ME doesn't support XML parsing as a part of the standard. There are XML parsers for Java ME though.

For such a simple requirement, XML would be an overkill. XML processing is pretty costly on resources. Many devices may not have enough resources. Also using XML file is no different from normal file and again problems with File I/O come into scene. RecordStore class is a standard class and devices can implement it in any way they want without the Java ME developers having to worry about the implementation details.
 
OP
RCuber

RCuber

The Mighty Unkel!!!
Staff member
^^ can I get some simple examples which suites the requirement using the RecordStore :)
 
OP
RCuber

RCuber

The Mighty Unkel!!!
Staff member
^^ Thanks for the link. This should do that trick.
few queries.
Where exactly is the file stored? in the example record name is db_1 where is it stored?.
he is using netbeans for testing (emulation) , and a WinMo & SE K320i as test mobiles.
 

chandru.in

In the zone
Where exactly is the file stored? in the example record name is db_1 where is it stored?.
he is using netbeans for testing (emulation) , and a WinMo & SE K320i as test mobiles.
RecordStore abstracts the implementation completely from the developer. Device manufacturers can implement RecordStore in anyway they want. In fact it may not even be a file. It may even be stored in magical air space within the device. ;)

Java ME (just like Java EE) gives only the specifications and applications are built to those specifications. How the implementation is done is completely upto the vendors.
 
OP
RCuber

RCuber

The Mighty Unkel!!!
Staff member
This is getting interesting post by post.... :D
I am not a java developer but still im interested to give it a try :D
My weekend plan is fixed :D
 
Status
Not open for further replies.
Top Bottom