作者:p欧阳宏生 | 来源:互联网 | 2023-06-21 09:56
把.txt文件放在assets文件夹中publicclassMainActivityextendsActivity{publicstaticfinalStringENCODIN
把 .txt文件 放在 assets文件夹中
public class MainActivity extends Activity {
public static final String ENCODING = "GBK";
private TextView teView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
teView = (TextView) findViewById(R.id.button1);
editText2 = (EditText) findViewById(R.id.editText2);
intent_two = (Button) findViewById(R.id.intent_two);
teView.setText(getFromAssets("xieyi.txt"));
public String getFromAssets(String fileName) {
String result = "";
try {
InputStream in = getResources().getAssets().open(fileName);
// 获取文件的字节数
int lenght = in.available();
// 创建byte数组
byte[] buffer = new byte[lenght];
// 将文件中的数据读到byte数组中
in.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
android:id="@+id/login_scroll"
android:layout_
android:layout_
android:fillViewport="true"
android:orientation="vertical" >
android:layout_
android:layout_
android:layout_marginTop="100dp" >
android:layout_
android:layout_ >
android:id="@+id/button1"
android:layout_
android:layout_ />