正在读取 android 中的 txt 文件...找不到文件

Reading txt files in android... File not found

这是我针对同一问题发起的第二个话题,因为我之前的问题没有得到正确的 answer/working 答案。我正在尝试从我的资产文件夹中读取一个文本文件。该代码在桌面上有效,但在 Android 上无效。我知道我需要上下文或资产管理器,但我无法获得解决方法。到目前为止,我已经尝试了所有方法,但找不到有效的教程。我什至将 java 版本更改为 1.7 以尝试使用资源。我仍然是菜鸟,所以任何帮助都会很棒。这里是完整的class读取文件并生成单词。

public class WordGenerator {

public String randomWord;
public Random rand;
public char [] randomWordChar;


public ArrayList<String> words = new ArrayList<String>();

private Context mCtx; //<-- declare a Context reference

public void WordGenerator() {


    rand = new Random();

    String all = new String("words.txt");
    String line;


    try {

        BufferedReader br = new BufferedReader(new FileReader(("words.txt")));
        if (!br.ready()) {
            throw new IOException();
        }
        while ((line = br.readLine()) != null) {
            words.add(line);
        }
        br.close();
    } catch (IOException e) {
        System.out.println(e);
    }
        int size = words.size();
        Random rn = new Random();
        int randWord = rn.nextInt(size);
        randomWord = words.get(randWord);
        randomWordChar = randomWord.toCharArray();
    }

}

我什至尝试过几次将加载过程分离到不同的 void class 但都失败了。如果可以帮忙谢谢。这是另一个问题的 link,因此您知道无效的答案!!

您应该使用 android.content.Context 让 "getAssets()" 方法起作用。

观察到您正在使用 javax.naming.Context 作为上下文。