簡體   English   中英

需要從android studio中assets文件夾中的txt文件中讀取

[英]Need to read from a txt file in the assets folder in android studio

我和我的朋友正在 Android Studio 中編寫一個簡單的應用程序。 當你按下一個按鈕時,一個新的活動會以你按下的按鈕的名稱打開,並顯示該文件中的文本。

我有生成第一組按鈕的代碼(這些是硬編碼的),我可以得到按下的按鈕的名稱。 我的麻煩是讀取文本文件並顯示內容。 文本文件中的每一行都是一個單詞,需要是按鈕的文本值。 我不能硬編碼這些詞,因為它們經常變化。

例子; 在主要活動中,您按下標記為“Round”的按鈕,它會將您轉到一個頁面,該頁面將名為“round”的文本文件中的所有單詞列為按鈕。

我之前問過這個問題,但因為太含糊而被擱置了。 我希望這更清楚。

這是我正在使用的代碼,但需要代碼來讀取文件。 這不正常。

我什至無法讓它顯示第一行。 文件內容是這樣的 --- Pipe Elbow Reducer Tap on flat EC

請幫忙。 提前致謝。

public class test extends Activity {
    int counter = 0;

    protected void onCreate(Bundle savedInstanceState) {
        counter = 0;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        TableLayout table = (TableLayout) findViewById(R.id.tblLayoutContent);

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(getAssets().open("round.txt")));

            // do reading, usually loop until end of file reading
            String mLine;
            while ((mLine = reader.readLine()) != null) {

                for (int row = 0; row < 10; row++) {
                    TableRow tblRow = new TableRow(this);
                    tblRow.setPadding(5, 30, 5, 5);
                    table.addView(tblRow);
                    int NUM_COL = 3;

                    for (int col = 0; col != NUM_COL; col++) {
                        Button btn = new Button(this);
                        btn.setText(mLine);
                        tblRow.addView(btn);
                        NUM_COL++;

                    }
                }

            }
        } catch (IOException e) {
            //log the exception
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    //log the exception
                }
            }
        }
    }
}

這是我的結構圖:

這是我的結構圖

好吧,我找到了答案。 謝謝你給我指明了正確的方向。 這里是

    try {
        InputStream is = getAssets().open("round.txt");

        // We guarantee that the available method returns the total
        // size of the asset...  of course, this does mean that a single
        // asset can't be more than 2 gigs.
        int size = is.available();

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

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

        // Finally stick the string into the text view.
        // Replace with whatever you need to have the text into.

        TextView tv = (TextView)findViewById(R.id.text);
        tv.setText(text);

    } catch (IOException e) {
        // Should never happen!
        throw new RuntimeException(e);
    }

重新編寫了代碼,這是有效的代碼。 BufferedReader 閱讀器; 嘗試 { InputStream is = getAssets().open("round.txt");

        reader = new BufferedReader(new InputStreamReader(is));

        // Finally stick the string into the text of the button.

        TableLayout table = (TableLayout) findViewById(R.id.tblLayoutContent);

        String line = reader.readLine();
        int lineLength = (line.length());
        while (line != null){

            TableRow tblRow = new TableRow(this);
            tblRow.setPadding(5, 30, 5, 5);
            table.addView(tblRow);


            for (int col = 0; col < NUM_COL; col++) {
                Button btn = new Button(this);
                btn.setTextSize(14);

                btn.setText(line);
                tblRow.addView(btn);
                line = reader.readLine();
            }
        };

    } catch (IOException e) {
        // Should never happen!
        throw new RuntimeException(e);
    }

}

試試這個...添加getResources()

reader = new BufferedReader(
                new InputStreamReader(getResources().getAssets().open("round.txt")));

你可以像這樣逐行讀取文件:

        String filename = "filename.txt";
        BufferedReader bufferedReader = null;    
        try {
            bufferedReader = new BufferedReader(new InputStreamReader
                    (this.getAssets().open(filename), StandardCharsets.UTF_8));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
              //add the lines in some arraylist if you want to set them.
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

在 Kotlin 中,我們可以這樣做

val string = requireContext().assets.open("round.txt").bufferedReader().use {
            it.readText()
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM