2011-08-16 10 views
2

1つのアクティビティ(Filewriter.java)で作成したファイルを、アプリケーション内の他のすべてのアクティビティ(データ操作用)に使用できるようにするにはどうすればよいですか?私は同じファイル名を使用するようにしましたが、どのように私はどこにでも同じファイルにアクセスしていますか?文字列名とその他の整数を含む2つのファイルに実装する必要があります。 writeUTF()を使って文字列ファイルを作成しました。複数のアクティビティからのファイルへのアクセス。

ここには、以前のアクティビティ(UI)からデータを受信し、その内容をファイルに書き込むためのコードがあります。私は私の主なジレンマが同じ一つのファイル(subject.txtまたはclasses.binかどうか)にアクセスできるようにすることですし、読むことができるように

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.  
    } 
    } 

...別のUIの活動から、この同じファイルにアクセスしたいです同じアプリ内の他のアクティビティからデータを操作することができます。

答えて

0

私はこのジレンマが何であるか分かりません。あなたが同じファイル名を使用していて、おそらく同じパス名を使用している場合は、定義によって同じファイルになります。

すべてのアクティビティが同じアプリ/パッケージに含まれている限り、問題は発生しないとは限りません。

したがって、2つのファイルのそれぞれに同じフルパス名を使用して、それらが同じファイルであるかどうかを心配する必要はありません。

openFileOutput("subject.txt", Context.MODE_WORLD_READABLE)(これは相対的なもので、ファイルの場所が分からない場合があります)を使用する代わりに、openFileOutput(getFilesDir () + "/" + "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(...); 

ます。また、開口部またはOutputStreamを閉鎖するために、あなたのApplication実装方法で宣言することができます。

+0

返事ありがとう!これを行こう! :) – zanazaar

関連する問題