Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Read Data From Raw Resource in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.66k
    Comment on it

    How to read raw data from raw resource in android. In this tutorial i am using a text file as a raw resource in res/raw/wordslist.txt .This text file is read by the activity and you can use the data whatever way you like. for eg

    You can display it in a list. or do any kind of searching or processing .

    So first make the android project ,then copy the text file in the resource folder res/raw/wordslist.txt , this is the text file you want to read in the activity . see the sample code for MainActivity

    package com.example.readtextfile;
    
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import android.app.Activity;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.widget.ListView;
    
    public class MainActivity extends Activity {
    
        ListView view;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            new Thread(new Runnable() {
                public void run() {
                    try {
                        loadList();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();
    
        }
    
        public void loadList()throws IOException
        {
            Log.d("TAG", "Loading words...");
            final Resources resources = this.getResources();
            InputStream inputStream = resources.openRawResource(R.raw.dic);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    
            try {
                String line;
                int i=0;
                while ((line = reader.readLine()) != null) {
    
                    if(line.startsWith("    "))
                    {
                        System.out.println("four letter ::"+line.substring(3, 8));
    
                    }
                    else
                    {
                        System.out.println("three letter ::"+line.substring(0,4));
                    }
                }
            } finally {
                reader.close();
            }
            Log.d("TAG", "DONE loading words.");
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: