【问题标题】:Accessing a file from multiple activities.从多个活动访问文件。
【发布时间】:2023-03-26 10:20:01
【问题描述】:

使我在一个活动(例如 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),并能够从同一个应用程序中的任何其他活动中读取和操作数据。

【问题讨论】:

    标签: android file-access


    【解决方案1】:

    我不确定这里的困境是什么 - 如果您使用相同的文件名,并且可能是相同的路径名,那么根据定义 - 它是同一个文件。

    只要您的所有活动都在同一个应用程序/包中,那么我认为您不会有任何问题。

    所以 - 为两个文件中的每一个使用相同的完整路径名,您不必担心它们是否是同一个文件。

    我建议您不要使用openFileOutput("subject.txt", Context.MODE_WORLD_READABLE)(它是相对的,您可能不知道文件在哪里),您可能希望使用openFileOutput(getFilesDir ( ) + "/" + "subject.txt", Context.MODE_WORLD_READABLE),它将始终在您的文件中为您提供完整的路径名应用在内部存储中的私有数据。

    我通常在我的应用程序中提供一个辅助方法:

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

    【讨论】:

      【解决方案2】:

      我建议使用Application 类来使您的所有活动都可以访问该文件。您可以扩展Application 类,并在您的自定义Application 类中创建您的DataOutputStream,并使其公开或通过getter 访问,getOutputStream()

      然后在您的活动中,您可以执行以下操作以访问OutputStream

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

      您还可以在您的Application 实现方法中声明用于在完成写入文件后打开或关闭OutputStream

      【讨论】:

        猜你喜欢
        • 2013-07-08
        • 2020-04-26
        • 2011-04-02
        • 2022-12-13
        • 1970-01-01
        • 2014-01-18
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        相关资源
        最近更新 更多