2

1 つのアクティビティ (Filewriter.java など) で作成したファイルを、アプリ内の他のすべてのアクティビティで (データ操作のために) 利用できるようにする最善の方法は何ですか? 必ず同じファイル名を使用するようにしましたが、どこからでも 1 つの同じファイルにアクセスしていることを確認するにはどうすればよいですか? これを 2 つのファイルに実装する必要があります。1 つは文字列名を含み、もう 1 つは整数です。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.       
    }
    }

私の主なジレンマは、同じ 1 つのファイル (subject.txt または classes.bin) にアクセスできるようにすることと、同じアプリ内の他のアクティビティからデータを読み取って操作できるようにすることです。

4

2 に答える 2

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;
}
于 2011-08-16T10:38:58.180 に答える
0

Applicationクラス内のすべてのアクティビティからファイルにアクセスできるようにするために、クラスを使用することをお勧めします。クラスを拡張Applicationし、カスタムApplicationクラスで を作成しDataOutputStream、パブリックにするか、getter を介してアクセスできるようにしますgetOutputStream()

次に、アクティビティで、にアクセスするために次のことを実行できますOutputStream

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

ファイルへの書き込みが完了したら、Application実装メソッドで を開いたり閉じたりすることも宣言できます。OutputStream

于 2011-08-16T11:24:00.323 に答える