-1

I want to read text in my raw resource folder

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String fileText = Utils.readRawTextFile(this, R.raw.foodlist);


        TextView text = (TextView) findViewById(R.id.mytext);


        text.setText(fileText);

    }

}

I copied a code from here

What is this Utils class, I also looked up here

I already configure its build path to alternate but instead there is no "readRawTextFile" method in the Utils class.

SOLVED :

I found it here

I had to create Utils.java which contain :

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;

public class Utils extends Activity {

    public static String readRawTextFile(Context ctx, int resId) {
        InputStream inputStream = ctx.getResources().openRawResource(resId);

        InputStreamReader inputreader = new InputStreamReader(inputStream);
        BufferedReader buffreader = new BufferedReader(inputreader);
        String line;
        StringBuilder text = new StringBuilder();

        try {
            while (( line = buffreader.readLine()) != null) {
                text.append(line);
                text.append('\n');
             }
         } 
         catch (IOException e) {
           return null;
         }
         return text.toString();
    }
}   

thanks everyone!

Community
  • 1
  • 1
eggblade
  • 3
  • 3

1 Answers1

0

You don't have an import for Utils class. Check if it is in your project. Also, you can look here to find working readRawTextFile method.

Community
  • 1
  • 1
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Yep, actually I copied from that question whereas I don't know what Utils is, I looked up on some question that says I had to configure build path, but that didn't work. In this case what is this Utils? – eggblade Jan 15 '14 at 15:02
  • My bad, I found that I had to set Utils.java, please look at my first post, it's edited. – eggblade Jan 15 '14 at 15:41