有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在使用参数时无法检索文件

在传递参数以搜索文件时,我遇到了一个问题。 基本上,当尝试读取文件时,以下代码起作用:

private void setContent(){
        String text = "";
        String randomText = "";

try
{

InputStream input = getAssets().open("files/jokes.txt");

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            // byte buffer into a string
            text = new String(buffer);

            String[] splitText = text.split("###");
            Random rand = new Random();
            int  randomIndex = rand.nextInt(splitText.length);
            randomText = splitText[randomIndex];

        }
        catch (Exception e) {
            System.out.println(e);
        }

        contentText.setText(randomText);

    }

注意上面的输入流。我的文本文件存储在assset/files/crooks中。txt

现在,我尝试使用一个意图,将文件名按原样进行参数化,当文件名经过时,它总是在catch语句中被捕获为“”:

主要活动:

jokesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            openContentPage("jokes");
        }
    });

    poemsButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            openContentPage("poems");
        }
    });

    funnyStoriesButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            openContentPage("funnyStories");
        }
    });

  private void openContentPage(String v) {
        if (v.equals("jokes")) {
            Intent intentContentPage = new Intent(MainActivity.this, Content.class);
            intentContentPage.putExtra("keyPage", "jokes");
            startActivity(intentContentPage);
        } else if (v.equals("poems")) {
            Intent intentContentPage = new Intent(MainActivity.this, Content.class);
            intentContentPage.putExtra("keyPage", "poems");
            startActivity(intentContentPage);
        } else if (v.equals("funnyStories")) {
            Intent intentContentPage = new Intent(MainActivity.this, Content.class);
            intentContentPage.putExtra("keyPage", "funnyStories");
            startActivity(intentContentPage);
        }

内容:

 private void setContent(){
        String text = "";
        String randomText = "";
        String keyPageValue = getIntent().getStringExtra("keyPage");

        String fileName = "";

        if(keyPageValue.equals("jokes") ){
            fileName.equals("files/jokes.txt");
        }
        else if (keyPageValue.equals("poems")){
            fileName.equals("files/poems.txt");
        }
        else if (keyPageValue.equals("funnyStories")){
            fileName.equals("files/funnystories.txt");
        }

        try {

            InputStream input = getAssets().open(fileName);

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            // byte buffer into a string
            text = new String(buffer);

            String[] splitText = text.split("###");
            Random rand = new Random();
            int  randomIndex = rand.nextInt(splitText.length);
            randomText = splitText[randomIndex];

        }
        catch (Exception e) {
            System.out.println(e);
        }

        contentText.setText(randomText);

    }

为什么它不选择它?如前所述,所有文件都存储在资产/文件文件夹中


共 (0) 个答案