is there any way out that we can make the data in text file persistent? everytime a user finishes playing a game, in my program his name and respective score is written to a text file. When the next player comes the previous one gets overwritten. since am writing in write mode, I am not sure whether append mode is supported to save scores of this sort in blackberry...any suggestions are welcome
Asked
Active
Viewed 972 times
0
-
1String text= cs.getText()+" "+(elapsed/34); outStream.write(text.getBytes()); cs.getText() gets the user's name and elapsed/34 generates score.These 2 entities are written properly but how can I make this work without disturbing the previous entries...so that when I visit the flat file I must get a list of names and their scores. – Rashmi.B Nov 30 '10 at 18:03
-
2As far as I know there isn't an append mode in BlackBerry but this should be simple to recreate. Just read in all of the textfile before you overwrite it and then write it back in when you go to write the file. However, I'm still curious as to why you haven't used the PersistentStore and a Hashtable like I originally recommended. I really think that's a better approach and if you post what problems you're having with that I will try my best to answer them. – Jonathan Nov 30 '10 at 18:39
-
As Jonathan mentioned; have a look at the PersistentStore : I wrote up an example of it's usage here: http://stackoverflow.com/questions/3805182/how-do-i-use-the-persistent-object-store-in-blackberry/3808672#3808672. It should be faster and simpler. – eSniff Nov 30 '10 at 19:20
1 Answers
3
You should really use the PersistentStore to store this type of information - it's much easier to use and probably more reliable than trying to write files.
However, if you insist on writing files, here's the general code to open a file for appending:
private OutputStream openFileForWriting(String filePath) {
try {
FileConnection fconn = (FileConnection) Connector.open(filePath);
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (!fconn.exists()) {
fconn.create(); // create the file if it doesn't exist
}
return fconn.openOutputStream(fconn.fileSize());
} catch (IOException ioe) {
System.out.println("Could not open " + filePath + " for writing");
}
return null;
}

Marc Novakowski
- 44,628
- 11
- 58
- 63
-
@ Jonathan: Alright now that you know how the scores and names are generated guide me as to how do i implement Hashtables in this...and..for that matter, each time on a new players entry the table should get autoupdated....When I visit the highscores icon I must be able to see all the list of names and their scores...please suggest as to how to do it. – Rashmi.B Dec 01 '10 at 12:05
-
You should really ask a new question for that but here's a brief explanation. Create a new hashtable and put the scores in like this: hashtable.put(
, – Jonathan Dec 01 '10 at 19:03). When you need a list of the scores call hashtable.keys(), loop through the keys and print the scores for each user.