Android文件读写简单示例
16lz
2021-01-23
界面如下
package org.snailteam;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class FileSave extends Activity {private EditText text;private Button save, reader;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.fileui);text = (EditText) findViewById(R.id.editText1);save = (Button) findViewById(R.id.buttonSave);reader = (Button) findViewById(R.id.reader);save.setOnClickListener(new OnClickListener() {public void onClick(View view) {OutputStream os = null;try {os = openFileOutput("textMe", MODE_PRIVATE);os.write(text.getText().toString().getBytes());} catch (FileNotFoundException e) {} catch (IOException e) {} finally {try {os.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}text.setText("");}});reader.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {text.setText("");InputStream is = null;byte[] b = null;try {is = openFileInput("textMe");b = new byte[1024];int length = is.read(b);text.setText(new String(b));} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}});}}
更多相关文章
- Android读写文件示例
- Android 的网络编程(5)-通过ksoap2调用webservice示例
- android 登陆界面
- android 滑动欢迎界面
- Android 主界面 九宫格 超灵活的配置
- Android情景模式、文件管理器 完整示例编程详解、Android程序优
- Android 监听Fragment界面的点击事件
- android 模拟软件启动界面
- Andoid - 开发实例(2): ViewPager卡片式界面2