写入 BlackBerry 中的文件

发布于 2024-10-04 21:39:41 字数 777 浏览 1 评论 0原文

嘿伙计们,我已将我的代码粘贴到这里。 Dialog.inform 包含一个“长”值,它是玩家的分数。如果我想将其写入文件,我需要将其转换为字节整数。我将垃圾值写入文件中。任何人都可以帮我解决这个问题吗? 的内容

我想打印 (elapsed/34) Dialog.inform("Congrats You Score : "+(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.exists())
         {
             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();

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.exists())
         {
             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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何其悲哀 2024-10-11 21:39:41

好的,既然您将分数保存到文件中,那么您可能希望该分数可以作为字符串查看。当您查看该文件时,我猜您会看到“Rashmi”一词,后面跟着一个垃圾字符。变量 lp 未正确打印的原因是数字的字符串值与其数值不同。例如,2 的二进制值为 10,而“2”的 ASCII 值为 00110010(或十进制的 50)。当您调用 outStream.write(lp) 时,这将打印 lp 的数值而不是其字符串值。简而言之,您需要将数值转换为字符串。尝试这样的事情:

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

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文