开发者

Writing to a file in BlackBerry

Hey Folks,I have pasted my code here. Dialog.inform contains a 'long' value which is a player's score. If i want to write it to a file, i need to convert it to int of byte.I am getting junk value written into the file.Can anyone help me out with this? I want to print the content of (elapsed/34)

Dialog.inform("Congrats You scored : "+(elapsed/34));

           try 
            {

         FileConnection fc = (FileConnection)Connector.open("file:///SDCard/Rashmi.txt");
         // If no exception is thrown, then the URI is valid, but the file may or may not exist.
         if (!fc.ex开发者_C百科ists())
         {
             fc.create();  // create the file if it doesn't exist
         }
         OutputStream outStream = fc.openOutputStream(); 
         int lp=safeLongToInt(elapsed/34);
         outStream.write("Rashmi".getBytes());
         outStream.write(lp);
         outStream.close();
         fc.close();


Okay, since you're saving the score to a file presumably you want that score to be viewable as a string. When you view that file I'm guessing that you see the word "Rashmi" followed by one junk character. The reason the variable lp is not being printed correctly is because the string value of a number is different from its numerical value. For instance the binary value of 2 is 10 whereas the ASCII value for '2' is 00110010 (or 50 in decimal). When you call outStream.write(lp) this is printing the numerical value of lp rather than its string value. In short, you need to cast the numerical value to a string. Try something like this:

String text = "Rashmi" + (elapsed/34);
outStream.write(text.getBytes());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜