# File IO in J2ME



## RCuber (Oct 10, 2008)

Hey guys, 
How can one store and retrive values from a textfile using J2ME. 
textfile will have content something like this 


```
#--Configuration File--
Value1=Hello
Value2=World
```

I need to read the values in the following format. 

```
string V1;
string V2;

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

For writing into the file I need the following format

```
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


----------



## chandru.in (Oct 10, 2008)

Ooops!

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

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.


----------



## mastermunj (Oct 10, 2008)

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 (Oct 10, 2008)

chandru.in said:


> 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 . 



> 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.




mastermunj said:


> 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  .. I read somewhere that J2ME does support it.


----------



## chandru.in (Oct 10, 2008)

Charan said:


> 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.



Charan said:


> @Chandru: Im am still curious about storing the values in a XML file and reading it  .. 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 (Oct 10, 2008)

^^ can I get some simple examples which suites the requirement using the RecordStore


----------



## chandru.in (Oct 10, 2008)

*www.java2s.com/Code/Java/J2ME/Readandwritetotherecordstore.htm


----------



## RCuber (Oct 10, 2008)

^^ 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 (Oct 10, 2008)

Charan said:


> 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 (Oct 10, 2008)

This is getting interesting post by post....   
I am not a java developer but still im interested to give it a try  
My weekend plan is fixed


----------



## chandru.in (Oct 10, 2008)

Welcome to the wonderful land of Java.  Java indeed rocks!


----------

