简体   繁体   English

从资产android读取文件

[英]read file from assets android

I'm trying to build a card viewer in android for a trading card game. 我正在尝试在Android中为交易纸牌游戏构建纸牌查看器。 My plan was to use an arraylist to populate the cards into the database. 我的计划是使用数组列表将卡填充到数据库中。 I would read in all values from file and would be displayed via listview. 我将从文件中读取所有值,并将通过listview显示。 My listview code and arraylist code is fine (after testing without using file input). 我的listview代码和arraylist代码很好(在测试后不使用文件输入)。 The problem occurs when I attempt to read in from file. 当我尝试从文件中读取时,会发生问题。 I'm at a loss of what to do. 我不知所措。 When I run the program like this, it crashes. 当我像这样运行程序时,它崩溃了。 When I remove the "parseInt" method (because i'm reading in an integer from file), it runs, but none of my data is populated. 当我删除“ parseInt”方法(因为我正在从文件中读取整数)时,该方法运行,但是没有填充我的数据。 What is wrong with my io code? 我的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 
            }
}...

Try this code to get data from the assets. 尝试使用此代码从资产中获取数据。

OR if you don't want to clear your code. 或者,如果您不想清除代码。 try modify your code like this. 尝试像这样修改您的代码。 I think the problem is in your loop. 我认为问题出在您的循环中。

String line = null;

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

        }

but collecting/storing data like this is unstable and hard to maintain. 但是像这样收集/存储数据是不稳定且难以维护的。 i'll suggest to use JSON. 我建议使用JSON。 to store data in asset,so you could be able to collect and store data as fast as possible. 将数据存储在资产中,以便您能够尽快收集和存储数据。

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

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