Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要读取android studio中资产文件夹中的txt文件_Java_Android - Fatal编程技术网

Java 需要读取android studio中资产文件夹中的txt文件

Java 需要读取android studio中资产文件夹中的txt文件,java,android,Java,Android,我和我的朋友正在Android Studio中编写一个简单的应用程序。当您按下按钮时,将打开一个新活动,其中包含您按下的按钮的名称,并显示该文件中的文本 我有生成第一组按钮的代码(这些是硬编码的),我可以得到按下的按钮的名称。我的问题是读取文本文件并显示内容。文本文件中的每一行都是一个单词,需要作为按钮的文本值。我不能硬编码这些单词,因为它们经常变化 榜样;在主活动中,您按下标记为“Round”的按钮,它会将您发送到一个页面,该页面将名为“Round”的文本文件中的所有单词列为按钮 我早些时候问

我和我的朋友正在Android Studio中编写一个简单的应用程序。当您按下按钮时,将打开一个新活动,其中包含您按下的按钮的名称,并显示该文件中的文本

我有生成第一组按钮的代码(这些是硬编码的),我可以得到按下的按钮的名称。我的问题是读取文本文件并显示内容。文本文件中的每一行都是一个单词,需要作为按钮的文本值。我不能硬编码这些单词,因为它们经常变化

榜样;在主活动中,您按下标记为“Round”的按钮,它会将您发送到一个页面,该页面将名为“Round”的文本文件中的所有单词列为按钮

我早些时候问过这个问题,但因为太模糊而被搁置。 我希望这更清楚

这是我正在使用的代码,但需要这些代码来读取文件。这是不对的

我甚至不能让它显示第一行。文件内容如下所示--- 管 肘部 减速器 轻敲平板 欧共体

请帮忙。 提前谢谢

公共类测试扩展活动{
int计数器=0;
创建时受保护的void(Bundle savedInstanceState){
计数器=0;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u content);
TableLayout table=(TableLayout)findViewById(R.id.tblLayoutContent);
BufferedReader reader=null;
试一试{
读卡器=新的BufferedReader(
新的InputStreamReader(getAssets().open(“round.txt”);
//进行读取,通常循环直到文件读取结束
弦线;
而((mLine=reader.readLine())!=null){
对于(int行=0;行<10;行++){
TableRow tblRow=新的TableRow(本);
tblRow.设置填充(5,30,5,5);
表.添加视图(tblRow);
int NUM_COL=3;
for(int col=0;col!=NUM_col;col++){
按钮btn=新按钮(此按钮);
btn.setText(多行);
tblRow.addView(btn);
NUM_COL++;
}
}
}
}捕获(IOE异常){
//记录异常
}最后{
if(读卡器!=null){
试一试{
reader.close();
}捕获(IOE异常){
//记录异常
}
}
}
}
}
这是我的结构图:


试试这个。。。添加getResources()

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

试试这个。。。添加getResources()

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

我找到了答案。谢谢你给我指明了正确的方向。 给你

    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);
    }

我找到了答案。谢谢你给我指明了正确的方向。 给你

    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);
    }

重新编写了代码,这是一个有效的。 缓冲读取器; 试一试{ InputStream=getAssets().open(“round.txt”)

reader=new BufferedReader(new InputStreamReader(is));
//最后将字符串粘贴到按钮的文本中。
TableLayout table=(TableLayout)findViewById(R.id.tblLayoutContent);
字符串行=reader.readLine();
int lineLength=(line.length());
while(行!=null){
TableRow tblRow=新的TableRow(本);
tblRow.设置填充(5,30,5,5);
表.添加视图(tblRow);
for(int col=0;col
重新编写了代码,这就是有效的代码。 缓冲读取器; 试一试{ InputStream=getAssets().open(“round.txt”)

reader=new BufferedReader(new InputStreamReader(is));
//最后将字符串粘贴到按钮的文本中。
TableLayout table=(TableLayout)findViewById(R.id.tblLayoutContent);
字符串行=reader.readLine();
int lineLength=(line.length());
while(行!=null){
TableRow tblRow=新的TableRow(本);
tblRow.设置填充(5,30,5,5);
表.添加视图(tblRow);
for(int col=0;col
你可以像这样逐行读取文件:

        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();
                }
            }
        }

你可以像这样逐行读取文件:

        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();
                }
            }
        }

在科特林,我们可以

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

在科特林,我们可以

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

你说的“工作不正常”是什么意思?您现在得到的输出是什么?没有。根本没有输出。txt文件位于assets文件夹中;去我所有的捕猎区看看我能不能得到更多的信息。运气不好,没有错误被抛出。好的,看了一眼后得到了这个。。System.err:java.io.FileNotFoundException:round.txt“工作不正常”是什么意思?您现在得到的输出是什么?没有。根本没有输出。txt文件位于assets文件夹中;去我所有的捕猎区看看我能不能得到更多的信息。运气不好,没有错误被抛出。好的,看了一眼后得到了这个。。System.err:java.io.FileNotFoundException:rou