save and load values from a txt file
Hi is it possible to save/load data into a txt file?
for example, i want a user to fill in a form and once finished, all details are saved in a txt file. i also want the program to retreive the information and load it into the program again for later uses. can that be possible? if so would i need to create the txt file inside the src directory so that when the application is build into a jar file it wil contain a txt file inside the jar file? thanks
[466 byte] By [
jonney69a] at [2007-11-26 21:43:49]

# 1
Save info: It is possible, if your phone supports JSR-75.No doubt, there is information about JSR on this forum.Load info: InputStream is= getClass().getResourceAsStream ("/text.txt");And then... read about InputStream class.
# 2
After the user enters the data in the form, you can save the values to RecordStore.
# 3
do most midp 2.0 phones support this jsr? i've been browsing various phoens and none of them specify this type of info, only if it supports java midp2?
how would i be able to retrieve data from a record store?
does a record store object gets destroyed when the application terminates ie does it only work on runtime?
if not than all the data could be lost when the application in terminated right?
# 4
Comments inlined:
> do most midp 2.0 phones support this jsr? i've been
> browsing various phoens and none of them specify this
> type of info, only if it supports java midp2?
Not all of the J2ME enable devices support File connection API's you need to check out the specs provided by the respective device manufacturer sites.
As far as RecordStores API's are concerned with all Java ME enabled device will have this as an mandatory API whether it be MIDP1.0/MIDP2.0.
> how would i be able to retrieve data from a record
> store?
Just read some tutorial on creating recordstores in J2ME from the web. It is quite a simple concept.
> does a record store object gets destroyed when the
> application terminates ie does it only work on
> runtime?
The recordstore object may get destroyed but the data that you store in the RecordStores will never since, it will be stored in flash/some persistant storage medium on the device.
> if not than all the data could be lost when the
> application in terminated right?
It will never but once you delete the MIDlet from ur device the RecordStores associated for ur application will be removed by the AMS.
~Mohan
# 5
Thanks for your advice. Im trying to understand the fundamentals of record store and from what i have read so far, a record store stores bytes of data. is their some sort of Primary key or index key to identify a particular record? and can i retrieve parts of a record, ie i create a record of some ones credit card details and later on i wish to retrieve on their expire date and card number only?
my code so far retreives values inputted by a user from a form: here is the code
public void save()
{
//method to save new card details
//name,card,type,cvv2,date;
try
{
rs = RecordStore.openRecordStore("MyCards",true);
//add card details in the record store
recordLength = rs.getSize();
ByteArrayOutputStream baos= new ByteArrayOutputStream();
DataOutputStream dos= new DataOutputStream( baos );
//name, card etc are strings of the values retreived from textbox's
dos.writeUTF(name);
dos.writeUTF(card);
dos.writeUTF(type);
dos.writeUTF(cvv2);
dos.writeUTF(date);
byte data[] = baos.toByteArray();
recordId = rs.addRecord(data,0 1,data.length);
//close record store
rs.closeRecordStore();
baos.close();
dos.close();
}
catch(RecordStoreException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
Thanks
update, tried my code and the save() seems to work fine but in my load() method i get a nullpointer exception error which indicates this line of code:
byte[] data = rs.getRecord(recordId);
Right another update: i have amanged to resolve the above problem by creating the record stream in the constructor of the class.
now i have anotehr problem. i am trying to put all of the contents of the record store into a 2d array using enumerators. when i try to compile my code i get the above error
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:322)
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:322)
below is my code for the load()
RecordEnumeration re = rs.enumerateRecords(null,null,false);
size = re.numRecords();
String [] []cards = new String[size] [5];
if(re.hasNextElement())
{
byte[] data = re.nextRecord();
ByteArrayInputStream bais= new ByteArrayInputStream(data);
DataInputStream dis= new DataInputStream(bais);
for(int i = 0;i < size; i++)
{
for(int j = 0; j<5;j++)
{
cards[i][j] = dis.readUTF();
}
}
}
}
catch(RecordStoreException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
# 6
Firstly the RMS is not a RDBMS that follows Dr. E.F. Codd's rules.
When you keep on adding records to the record store it creates record id sequentially. If you say addRecord() for the first time then it will assign that data with record ID 1 and second addRecord() with record ID 2 and so on . . . ID 3 . . ID N
It is advisable to update the record instead of deleting. Anyways, from your code I found that you are maintaining only one record in the recordstore then it will always be record id 1. You do not have to enumerate records either in that case.
One more query I had was are you developing the app for production or for demo purpose? I it is not advisable to store the financial information like the creditcard # etc. with only streams without encrypting the data been stored.
I reckon the exception that you are getting must be a runtime one but not compile time. Kindly confirm.
~Mohan
# 7
its a demo im making that will turn into the real thing. what i want to do is to store the cards name and maybe the last 4 digits of the card on the phone whilst the rest of the details gets tranfered into a website.
some of the card details will be stored so that a user can select the card in which they want to use and then send a request to some server telling them that this is the card i will use.
anyways in regards to the error, yea its a runtime error. How can i maintain more than one record?
# 8
Here is a link to generic object that reads and stores properties in the record store. Its similar to the java.util.Properties class. I can also parse properties from a text file. http://worlddeveloper.org/www/forumtopicview.html?fid=42&categoryId=36&fpn=1
# 9
thanks for that link but i rather use record store or create a txt file has i've already started coding on it. I just need to fix this error message i get at runtime. i can succesfuly add data into a record store but retreiving it is rather difficult for me which should not be. when i do get the data from the record stre i want to store it in a 2d array
anyways thanks and hope that someone can help me in finding a solution to this runtime error...
# 10
Is there anyone that can give me some suggestions on how to fix this error? thanks and hope that someone can help me in this runtime error has i cant figure out why its giving me this error
# 11
In order to understand/eliminate the exception that you are getting you need to look at the java documention for DataOutputStream class writeUTF() method.With that you must definitely be able to rectify that exception.~Mohan
# 12
their is no writeUTF method under the dataOutputStream
# 13
Then how did this piece of code sent by u earlier compile?
> DataOutputStream dos= new
> DataOutputStream( baos );
>
> d etc are strings of the values retreived from
> textbox's
> dos.writeUTF(name);
> dos.writeUTF(card);
> dos.writeUTF(type);
> dos.writeUTF(cvv2);
> dos.writeUTF(date);
> byte data[] = baos.toByteArray();
~Mohan
# 14
for some odd reason it comiled and run fine without me changing anything! will do more tests to see what happened. instead of adding all the values retreived from all the textfields into one record, i will create a record for each value retrieved from the textfield
# 15
hi does anybody know where the actual record store file is kept?
# 16
Good idea !Must work but a bit of performance overhead.~Mohan
# 17
Do you mean on the device/ the emulator?~Mohan
# 18
yea on both the emulator and the device. I managed to add each retreive value into its own record store making it easier for me to retreive specific values from it. it worked like a treat.
# 19
Thats gr8.Regarding the emulator it resides in a forlder named "appdb" if I'm not mistaken it correctly. I do not know on the device but it depends on the device manufacturer to set the path while porting.~Mohan
# 20
i cant seem to find the appdb folder. is it inside my project folder? if so i cannot locate it there
# 21
its ok i found where its located at, thanks