android 40 Io编程
2015-10-18 17:01
387 查看
Io编程:内存卡和sd卡。字符串存入内存卡然后读出来。
activity:
页面
activity:
package com.sxt.day06_06; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText met;//文本框 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setListener(); } private void setListener() { setSaveDataClickListener(); setReadDataClickListener(); } private void setReadDataClickListener() { findViewById(R.id.btnReadData).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FileInputStream in =null; try { in= openFileInput("file.dat"); byte[] data=new byte[1024]; int len = in.read(data);//返回实际读取的字节数 String text=new String(data, 0, len, "utf-8"); Toast.makeText(MainActivity.this, text, 3000).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }); } private void setSaveDataClickListener() { findViewById(R.id.btnSaveData).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { FileOutputStream out = null; try { out=openFileOutput("file.dat", MODE_PRIVATE); String text=met.getText().toString(); byte[] data=text.getBytes("utf-8");//FileOutputStream时data要转换为字节数组 out.write(data); out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(out!=null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } }); } private void initView() { met=(EditText) findViewById(R.id.et); } }
页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="哈罗,张飞!"/> <Button android:id="@+id/btnSaveData" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存数据" /> <Button android:id="@+id/btnReadData" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取数据" /> </LinearLayout>
相关文章推荐
- Android Context对象
- Android APK反编译
- Android Listview的分组实现
- 【Android应用开发技术:应用组件】Handler组件通信机制
- Android开发学习日记
- android 布局如何支持多种不同屏幕尺寸
- android 39 共享首选项
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- ANDROID L——RecyclerView,CardView进口和使用(Demo)
- Android基础入门教程——10.2 SmsManager(短信管理器)
- Android Support v4、v7、v13 介绍
- Android--PullToRefreshListView 的简单使用
- Android系统性能优化总结
- android 38 Abdroid客户端和服务端交互
- 关于Android的asynctask-threads-limits问题:asynctask开启的线程是否有极限
- Android通知之自定义通知、响应通知上自定义按钮和更新通知的实现
- Android下拉刷新上拉加载控件,对所有View通用!
- Android-----第三方 ImageLoader 的简单配置和使用
- 使用git查阅Android M中Doze相关代码
- Android应用程序资源文件的编译和打包原理