Reading Android source text file

I have a words.txt file that I placed in my res / raw folder. Words in the file are separated by a space. I find it difficult to write Android / Java code to read a file in a word.

+7
source share
5 answers
 //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()); } } 
+5
source

Reading 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(); } 
+4
source

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 a space (including space). If you want it to start only when using the s.useDelimiter(" "); space s.useDelimiter(" "); after its creation.

+4
source

To retrieve words from a file from a raw folder, try the following approach

Read data from the source folder using getResources().openRawResource(R.raw.song);
Then get the input stream data into an array of bytes, share 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

+3
source

I had the same question, and although the answers above are probably correct, I could not get them to work successfully. This is almost certainly an operator error and ignorance on my part - but just in case someone wants to see me - n00b - finally do 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 quite sure why this worked for me when it wasn’t higher, because it does not look fundamentally different, but, as I said, it was probably an operator error on my part.

+2
source

All Articles