DIGIT-ARC read-only archive · snapshot: July 23, 2026

File IO in J2ME

11 posts · originally from https://geek.digit.in/community/threads/file-io-in-j2me.99505/

RCuber
RCuber Original post #1
Oct 10, 2008 at 12:02 PM
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 ;)
C
Oct 10, 2008 at 5:04 PM
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.
M
Oct 10, 2008 at 5:54 PM
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...
RCuber
RCuber #4
Oct 10, 2008 at 6:14 PM
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.
C
Oct 10, 2008 at 6:20 PM
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.
RCuber
RCuber #6
Oct 10, 2008 at 6:22 PM
^^ can I get some simple examples which suites the requirement using the RecordStore :)
C
Oct 10, 2008 at 6:26 PM
*www.java2s.com/Code/Java/J2ME/Readandwritetotherecordstore.htm
RCuber
RCuber #8
Oct 10, 2008 at 7:07 PM
^^ 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.
C
Oct 10, 2008 at 7:12 PM
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.
RCuber
RCuber #10
Oct 10, 2008 at 7:36 PM
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
C
Oct 10, 2008 at 7:45 PM
Welcome to the wonderful land of Java. Java indeed rocks! :)