2010-11-27 65 views
0

嗨伙计们,我在这里粘贴了我的代码。 Dialog.inform包含一个“长”值,这是一个玩家的分数。如果我想写它到一个文件,我需要将它转换为字节的int。我正在将垃圾值写入文件。可以帮助我解决这个问题吗?我想打印内容(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(); 

回答

1

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

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

再次谢谢...乔纳森...它nworked :)我现在可以显示名称和分数:) – 2010-11-27 18:22:15