2010-11-27 63 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