开发者

Reading Android raw text file

I have a words.txt file which I have placed in my res/raw folde开发者_如何学Gor. The words in the file are separated by space. I'm having a hard time writing the Android/Java code to read the file word by word.


Read from res/raw folder to String

InputStream inputStream = getResources().openRawResource(R.raw.yourtextfile);
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream));
String eachline = bufferedReader.readLine();
while (eachline != null) {
    // `the words in the file are separated by space`, so to get each words 
    String[] words = eachline.split(" ");
    eachline = bufferedReader.readLine();
}


//put your text file to raw folder, raw folder must be in resource folder. 

private TextView tv;
private Button btn;

    btn = (Button)findViewById(R.id.btn_json);
    tv = (TextView)findViewById(R.id.tv_text);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            SimpleText();


        }
    });

private void SimpleText(){

    try {


        InputStream is = this.getResources().openRawResource(R.raw.simpletext);
        byte[] buffer = new byte[is.available()];
        while (is.read(buffer) != -1);
        String jsontext = new String(buffer);



        tv.setText(jsontext);

    } catch (Exception e) {

        Log.e(TAG, ""+e.toString());
    }

}


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


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.


I had this same question and while the above answers are probably correct I was unable to get them to work successfully. This was almost certainly operator error and ignorance on my part - but just in case someone wants to see how I - a n00b - finally did it, here is my solution:

// this is just the click handler for a button...
public void loadStatesHandler(View v) {
    try {  
        String states = getStringFromRaw(this);
        readOutput.setText(states);
    } 
    catch(Throwable t) {
        t.printStackTrace();
    }
}

private String getStringFromRaw(Context c) throws IOException {
        Resources r = c.getResources();
        InputStream is = r.openRawResource(R.raw.states);
        String statesText = convertStreamToString(is);
        is.close();
        return statesText;
}

private String convertStreamToString(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = is.read();
    while (i != -1) {
        baos.write(i);
        i = is.read();
    }
    return baos.toString();
}

I'm not exactly sure why this worked for me when the above didn't as it doesn't seem fundamentally different - but as I said - it was probably operator error on my part.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜