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!