Reading a random line from text file in android

+1 Usman Mohy ud Din · October 17, 2014
Hy EveryOne...

I want to read a random line from .txt file in android.I am using following code.
My file contain almost 1,50,000 words(one word per line).
Please guide me
Thanx in advance...
        

public class MainActivity extends Activity {
  
TextView helloTxt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        helloTxt = (TextView)findViewById(R.id.tv);
        helloTxt.setText(readTxt());
    }
    
    private String readTxt(){`

    InputStream inputStream = getResources().openRawResource(R.raw.data);
     
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   
     
     int i;
     try {
   i = inputStream.read();
  while (i != -1)
      {
       byteArrayOutputStream.write(i);
       i = inputStream.read();
      }
      inputStream.close();
 } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
    return byteArrayOutputStream.toString();
    }
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Usman Mohy ud Din · October 18, 2014
Sorry dude

I did not get what you trying to say. :(
Please tell me in detail.
Kind Regards
  • 1

Java / Android Development

107,263 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator