Reading Android raw text file

前端 未结 5 781
长发绾君心
长发绾君心 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:17

    To get the words from the file from raw folder try with the following approach

    Read the data from raw folder using getResources().openRawResource(R.raw.song);
    Then get the inputstream data in a byte array split the data with space.

    Use the following code

    InputStream is =getResources().openRawResource(R.raw.song);
                BufferedInputStream bis = new BufferedInputStream(is);
    
                ByteArrayBuffer baf = new ByteArrayBuffer(50);
    
                int current = 0;
    
                while ((current = bis.read()) != -1) {
    
                    baf.append((byte) current);
    
                }
    
                byte[] myData = baf.toByteArray();
                String dataInString = new String(myData);
                String[] words = dataInString.split(" ");
    

    Thanks Deepak

提交回复
热议问题