简体   繁体   English

创建一个单独的方法以从Assets文件夹中的子文件夹读入文本文件

[英]Creating a separate method to read in text files from a subfolder in the assets folder

This is my first time on here, so I'm bit nervous and please forgive me if I don't seem entirely clear about what I'm asking. 这是我第一次来这里,所以我有点紧张,如果我对自己的问题不太清楚,请原谅我。

The problem is, Im trying to read in a file from a subfolder in the assets folder, using a method that I've created in a separate class. 问题是,我试图使用我在单独的类中创建的方法,从Assets文件夹中的子文件夹中读取文件。 I've researched this for a couple of days but I'm unable to find the solution anywhere, so I've come here as a last resort. 我已经研究了几天,但无法在任何地方找到解决方案,因此我来这里是不得已的方法。 I needed the file reading method to be separate as there are other views/activities that will be utilising exactly the same method and I don't think it would be wise to keep copying and pasting the same code for each activity. 我需要将文件读取方法分开,因为还有其他视图/活动将使用完全相同的方法,并且我不认为为每个活动复制和粘贴相同的代码是不明智的。 Ok here's what I've done so far: 好的,这是我到目前为止所做的:

public class ReadAssets extends Activity {


public void read(Context context, String filepath, int textviewid) {

    try {
        InputStream input = getAssets().open(filepath);

        int size = input.available();

        // Read the entire asset into a local byte buffer.
        byte[] buffer = new byte[size];
        input.read(buffer);
        input.close();

        // Convert the buffer into a string.
        String text = new String(buffer);

        // Finally insert the string into the text view.
        TextView tv = (TextView) findViewById(textviewid);
        tv.setText(text);

    } catch (IOException e) {
        // Throws an exception if an error is found
        throw new RuntimeException(e);
    }


  }
}

The activity that I would like to place this method in: 我想将此方法放入的活动:

public class GeneralSetupActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gettingstarted_layout);


    ReadAssets nA = new ReadAssets();
    nA.read(this,"gettingstarted/GettingStarted.txt", R.id.displayTextView);

//  try {
//  InputStream input =getAssets().open("gettingstarted/GettingStarted.txt");
//
//  int size = input.available();
//
//  // Read the entire asset into a local byte buffer.
//  byte[] buffer = new byte[size];
//  input.read(buffer);
//  input.close();
//
// // Convert the buffer into a string.
// String text = new String(buffer);
//
//          // Finally insert the string into the text view.
//          TextView tv = (TextView) findViewById(R.id.displayTextView); 
//          tv.setText(text);
//          
//      } catch (IOException e) {
//          // Throws an exception if an error is found
//          throw new RuntimeException(e);
//      }
      }

}

I'd really appreciate it file someone could point me towards the right direction. 我真的很感谢有人向我指出正确的方向。 And also I hope I'm not taking advantage but I'd like to know how I'd import and display a series of text files, one after another. 另外,我希望我没有利用自己的优势,但我想知道如何导入和显示一系列文本文件,一个接一个。

Cheers Guys, Thanks :) 大家好,谢谢:)

If you need this available to all different types of Activity, you should consider putting the method in a superclass so all the children can use it. 如果需要所有不同类型的Activity都可以使用此方法,则应考虑将方法放在超类中,以便所有子代都可以使用它。

public class ExtraFunctionalActivity extends Activity
{
    public void read(...)
    {        
        //your code
    }
}

public class GeneralSetupUtility extends ExtraFunctionalActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gettingstarted_layout);

        read(this,"gettingstarted/GettingStarted.txt", R.id.displayTextView);

    }
}

Otherwise, if this method is needed by a bunch of unrelated classes, you could put it in a utility class; 否则,如果一堆不相关的类都需要使用此方法,则可以将其放在实用程序类中;否则,请使用实用程序类。

public class FileUtil
{

    public static void read(...)
    {
        //your code
    }

}

then you can call it where needed with 然后您可以在需要的地方调用它

FileUtil.read(args here);

You may have a class like Fileparsingutility with the method you want to separate. 您可能具有类似Fileparsingutility的类,并带有要分离的方法。 You may define Inputstream as parameter(you may pass other required things as parameter for that method). 您可以将Inputstream定义为参数(可以将其他必需的东西作为该方法的参数传递)。 What ever actvity you want to use this method, instantiate above class and invoke the method by passing parameters. 无论您想使用哪种方法,都要实例化上述类并通过传递参数来调用该方法。

Fileparinsgutility util=new Fileparsingutility(); Fileparinsgutility util = new Fileparsingutility(); Returnobj retObj =util.parse(......); Returnobj retObj = util.parse(......);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM