从资产中读取文件 android

read file from assets android

我正尝试在 android 中为集换式纸牌游戏构建纸牌查看器。我的计划是使用数组列表将卡片填充到数据库中。我将从文件中读取所有值并通过列表视图显示。我的 listview 代码和 arraylist 代码很好(在不使用文件输入的情况下进行测试后)。当我尝试从文件中读入时出现问题。我不知所措。当我 运行 这样的程序时,它崩溃了。当我删除 "parseInt" 方法时(因为我正在从文件中读取一个整数),它 运行s,但是 none 我的数据被填充了。我的 io 代码有什么问题?

    import android.content.Intent;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;

    import java.util.ArrayList;
    import java.io.*;

    import android.widget.ArrayAdapter;
    import android.widget.ListView;


    public class DeckBuilderActivity extends ActionBarActivity {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
     Intent intent = getIntent();
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_deck_builder);

    //populate       database************************************************************************
    ArrayList<Card> database = new ArrayList<Card>(); //create new arraylist of datatype card

    //value holders
    String cardName, expansion, cardNum, unitType, unitClan, unitRace, trigger, unitSkill, cardLore,
            imageId;
    int unitGrade, unitPower, unitShield;
    //exception handling

    try{
        String myfile = "cardmaindatabase.txt";
        InputStream inputReader = getAssets().open(myfile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputReader)); //open file
        //begin populating with a for loop
        int i = 0; //incrementer
        while(reader.readLine() != null) //while not end of file
        {
            //assign values to placeholders

            //add new card and take parameters
            database.add(new Card());
            database.get(i).setCardName(reader.readLine());
            database.get(i).setExpansion(reader.readLine());
            database.get(i).setCardNum(reader.readLine());
            database.get(i).setUnitGrade(Integer.parseInt(reader.readLine()));





        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    //******************************************************************************************
    //populate arraylist
    ArrayAdapter<Card> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, database);
    ListView lv = (ListView) findViewById(R.id.card_database_listview);
    lv.setAdapter(adapter);


}
        StringBuffer stringBuffer = new StringBuffer();

        try {

            InputStream is = mContext.getResources().getAssets().open("your-file-path");
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line;

            while ((line = br.readLine()) != null) {
                //play with each line here 
            }
}...

尝试使用此代码从资产中获取数据。

或者如果您不想清除代码。尝试像这样修改您的代码。我认为问题出在你的循环中。

String line = null;

while((line = reader.readLine()) != null) //while not end of file
        {
            //play with each line here

        }

但 collecting/storing 这样的数据不稳定且难以维护。我建议使用 JSON。将数据存储在资产中,这样您就可以尽快收集和存储数据。