我想从 res/raw中加载一个text文件。我已经看了一些代码片段但是没有找到能实现的方法。
TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
helloTxt.setText(readTxt());
}
private String readTxt() {
InputStream inputStream = getResources().openRawResource(R.raw.hello);
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();
a)(TextView)findViewById(R.id.hellotxt);这行提示,Eclipses推荐移除代码。
b)getResources()这行提醒我添加 getResources()方法。
这是一个独立的类文件,我在 public String returnPass(){}方法调用 public class PassGen{}来实现这个独立的类文件。