2011-08-16 54 views
2

什麼是最好的方式來創建一個文件,我創建了一個活動(說Filewriter.java)可用(數據操作)到我的應用程序中的所有其他活動?我已經確保使用相同的文件名,但我如何確保我在任何地方訪問同一個文件?我需要在2個文件上實現它,一個包含字符串名稱和其他整數。我用writeUTF()創建了字符串文件;從多個活動訪問文件。

這裏是從前一個活動(UI)接收數據並將內容寫入文件的代碼。我想從另一個UI活動訪問同一個文件...

package com.shuaib669.bunkrecord; 

import java.io.DataOutputStream; 
import java.io.IOException; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 

public class filewriter extends Activity{ 

    String[] newText = new String[7]; 
    String[] newNum = new String[7]; 

    int[] no_of_classes = new int[7]; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 


     Bundle extras = getIntent().getExtras(); 
     newText = extras.getStringArray("com.shuaib669.bunkrecord.thetext"); 
     newNum = extras.getStringArray("com.shuaib669.bunkrecord.thenum"); 
     finish(); 

     if (newText != null && newNum != null){ 

      try { 
         // open subject.txt and classes.bin for writing 
       DataOutputStream out1 = new DataOutputStream(openFileOutput("subject.txt", Context.MODE_WORLD_READABLE)); 
       DataOutputStream out2 = new DataOutputStream(openFileOutput("classses.bin", Context.MODE_WORLD_READABLE)); 

       for(int x=0;x<7;x++){ 

        try{ 
       //converting string representation of no.s into integers 
      no_of_classes[x]=Integer.parseInt(newNum[x]); 
        } 
        catch(Exception e){ 
         e.printStackTrace(); 
        } 
       } 
       for(int x=0;x<7;x++){ 

        if(newText[x]==null) 
         out1.writeUTF("\n"); 
        else if(newText[x]==" " || newText[x]=="\n" || newText[x]=="\t") 
         out1.writeUTF("\n"); 
        else 
         out1.writeUTF(newText[x]); 


        if(newNum[x]==null) 
         out2.writeInt((Integer) null); 
        else 
         out2.writeInt(no_of_classes[x]); 


       } 

       out1.close(); 
      } 
       catch(IOException e){ 
        Log.e("Data Input Sample", "I/O Error"); 
       } 
       } 


    startNextMatchingActivity(new Intent("com.shuaib669.bunkrecord.SUBLIST")); 
    //Sublist being the activity that displays the stored contents of the file and lets user manipulate the file classes.bin.  
    } 
    } 

我的主要困境是能夠訪問相同的一個文件(不論subject.txt或classes.bin),並能閱讀並在同一應用中操縱來自其他任何活動的數據。

回答

0

我不確定這裏有什麼困難 - 如果您使用相同的文件名,並且可能是相同的路徑名,那麼根據定義 - 它是相同的文件。

只要你所有的活動都在同一個應用程序/包中,那麼我不會看到你會有任何問題。

因此 - 爲這兩個文件中的每一個使用相同的完整路徑名,並且您不必擔心它們是否是相同的文件。

我會推薦的是,您可能想要使用openFileOutput(getFilesDir () + "/" + "subject.txt", Context.MODE_WORLD_READABLE),而不是使用openFileOutput("subject.txt", Context.MODE_WORLD_READABLE)(這是相對的,而且您可能不知道文件的位置),它會始終爲您提供應用程序私有數據中的完整路徑名內部存儲。

我通常在我的應用程序提供一個輔助方法:

private String getFileName (String file) { 
    return getFilesDir () + "/" + file; 
} 
+0

嘿感謝您的回覆!會給這個去! :D – zanazaar

0

我會建議使用Application類讓你從內所有活動訪問的文件。您可以擴展Application類,並在您的自定義Application類中創建DataOutputStream,並將其設爲公共或通過吸氣器getOutputStream()訪問。

然後在你的活動,你可以做以下爲訪問OutputStream

((MyApplication)getApplication()).getOutputStream().write(...); 

你也可以在你的Application實現方法聲明爲打開或關閉OutputStream,一旦你與寫入文件來完成。

+0

嘿謝謝你的回覆!會給這個去! :) – zanazaar