2012-05-03 4 views
4

私のプログラムのいくつかの点数を保存するためにファイルを使用していますが、問題は印刷できないということです。私はいくつかのことをしようと試みたが、私は自分のコードで正しいものを見つけることができません:バイトから文字列まで

try{ 
     String FILENAME = "FrogScoreFile"; 
     FileInputStream fos = openFileInput(FILENAME); 
     byte[] b = new byte[1]; 
     fos.read(b); 
     fos.close(); 
     String score= String b; 
     game5HighScore.setText(b); 
     }catch (java.io.FileNotFoundException e) { 
     } catch (IOException e) { 
       e.printStackTrace(); 
    } 
+1

youreのがやって道は、この '文字列のスコア=列B wrong..changeで'とところで、あなたは '必要がありますgame5HighScore.setText(スコア) ;と 'game5HighScore.setText(b);ではない – COD3BOY

答えて

15

あなたはnew stringオブジェクトを作成することによって、文字列にByte arrayを変換することができます。 =新しい文字列(b)の文字列のスコア `へ`;

byte[] b = new byte[1]; 
fos.read(b); 
fos.close(); 
String message = new String(b); 
0
try{ 
     String FILENAME = "FrogScoreFile"; 
     FileInputStream fos = openFileInput(FILENAME); 
     BufferedReader br = new BufferedReader(new InputStreamReader(fos)); 
     String yourText = br.readLine(); 
     br.close(); 
     game5HighScore.setText(yourText); 
    }catch (java.io.FileNotFoundException e) { 
    } catch (IOException e) { 
      e.printStackTrace(); 
} 

ところで。 SharedPreferencesまたはSQLiteデータベースでスコアを保存してみませんか?

SharedPrefrences Guide.

public static void saveScore(Context context) { 
    final SharedPreferences.Editor editor = context.getSharedPreferences(
      "settings", Context.MODE_PRIVATE).edit(); 
    editor.putInt("score", 100); 
    editor.commit(); 
} 

public static int readScore(Context context) { 
    final SharedPreferences sp = context.getSharedPreferences("settings", 
      Context.MODE_PRIVATE); 
    return sp.getInt("score", 0); 
} 
+0

私は本当にsharedpreferencesを取得しない、私は良いガイドを見つけることが原因でない – stevedc

+0

私は答えを編集します。あなたはsharedprefrenceガイドを見つけることができます。 –

+0

私はすでにそれを読んだが、私はそれを得るのをやめていないgoogleからのもの。私はそれを見て良い例が必要です – stevedc

関連する問題