Reading Android raw text file

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

    //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());
        }
    
    }
    

提交回复
热议问题