将事先编辑好的assets_text.txt,放入android项目的assets文件夹中,如下图:
用下面的代码读取assets_text.txt文件夹中的内容:
package com.qing.jsondemo;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
try {
// Return an AssetManager instance for your application's package
InputStream is = getAssets().open("assets_text.txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String text = new String(buffer, "GB2312");
textView.setText(text);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}