2014-02-24 108 views
0

我在Android中創建一個小遊戲,現在我想用一個文件作爲存檔。 開始時它會檢查是否已經存在遊戲存檔,如果沒有,則應該創建它。如果有一個存檔遊戲,它應該讀取它。當您關閉應用程序/關閉手機/等。它應該保存它。 爲了編碼,我搜索了一下並找到了一些東西,但它似乎不起作用,但我不知道爲什麼。這裏是我的類的相關片段:在內部存儲器上創建,讀取和保存文件(Android)

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    setContentView(R.layout.activity_main); 

    FileStart(); 


} 

public void FileStart() { 

    File external = getFilesDir(); 
    String path = external.getPath(); 
    File file = new File(path + "inv.txt"); 

    if(file.exists()) { 
     //read files 
     try { 
      BufferedReader in = new BufferedReader(new FileReader(file)); 
      String result = in.readLine(); 
      String[] resultSplit = result.split("|"); 
      fruits = Integer.parseInt(resultSplit[0]); 
      milk = Integer.parseInt(resultSplit[1]); 
      sugar = Integer.parseInt(resultSplit[2]); 
      wheat = Integer.parseInt(resultSplit[3]); 
      cake = Integer.parseInt(resultSplit[4]); 

      in.close(); 

      TextView tView = new TextView(this); 
      tView=(TextView)findViewById(R.id.textView1); 
      tView.setText(fruits + " x Fruits"); 
      tView=(TextView)findViewById(R.id.textView3); 
      tView.setText(milk + " x Milk"); 
      tView=(TextView)findViewById(R.id.textView4); 
      tView.setText(sugar + " x Sugar"); 
      tView=(TextView)findViewById(R.id.textView5); 
      tView.setText(wheat + " x Wheat"); 

      tView=(TextView)findViewById(R.id.textView9); 
      tView.setText(cake + " Cakes"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } else { 
     try { 
      file.createNewFile(); 
      FileWriter filewriter = new FileWriter(file); 
      BufferedWriter out = new BufferedWriter(filewriter); 
      out.write("0|0|0|0|0"); 
      out.flush(); 
      out.close(); 
      filewriter.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 



} 

@Override 
protected void onPause() { 
    super.onPause(); // Always call the superclass method first 
    //save game progress 
    File external = getFilesDir(); 
    String path = external.getPath(); 
    File file = new File(path + "inv.txt"); 
    try { 
     FileWriter filewriter = new FileWriter(file); 
     BufferedWriter out = new BufferedWriter(filewriter); 
     String toWrite = fruits + "|" + milk + "|" + "|" + sugar + "|" + wheat + "|" + cake; 
     out.write(toWrite); 
     out.flush(); 
     out.close(); 
     filewriter.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

是文件產生的?你面臨的問題是什麼?什麼不起作用? – Skynet

+0

看來該文件沒有創建。我是否必須創建「getFilesDir()」的文件夾?「第一? – fmaster

+0

Yes ofcourse,getFilesDir();會給你的應用程序文件夾,即在內部存儲器和它的合適的,當用戶卸載你的應用程序,該文件夾也將被刪除。 – Skynet

回答

0

使用下面的代碼創建一個目錄:

boolean success = (new File(getApplicationContext().getFilesDir()+"/YourFolderName")).mkdir(); 
     if (!success){ 
      Log.w("directory not created", "directory not created"); 
     } 
相關問題