Reading Android raw text file

前端 未结 5 772
长发绾君心
长发绾君心 2020-12-16 01:44

I have a words.txt file which I have placed in my res/raw folder. The words in the file are separated by space. I\'m having a hard time writing

5条回答
  •  再見小時候
    2020-12-16 02:10

    The simplest is to use a Scanner.

    Scanner s = new Scanner(getResources().openRawResource(R.raw.text_file));
    
    try {
        while (s.hasNext()) {
            String word = s.next();
            // ....
        }
    } finally {
        s.close();
    }
    

    The default delimiter is whitespace (including space). If you want it to only trigger on space use s.useDelimiter(" "); after creating it.

提交回复
热议问题