Android文件保存和读取
来源:互联网 发布:网络炒作方案 编辑:程序博客网 时间:2024/04/30 03:59
- public class DataActivity extends Activity {
- private EditText filenameText;
- private EditText contentText;
- private TextView resultView;
- private static final String TAG = "DataActivity";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- filenameText = (EditText) this.findViewById(R.id.filename);
- contentText = (EditText) this.findViewById(R.id.content);
- resultView = (TextView) this.findViewById(R.id.result);
- String filename = filenameText.getText().toString();
- Button button = (Button) this.findViewById(R.id.button);
- Button showButton = (Button) this.findViewById(R.id.showButton);
- button.setOnClickListener(listener);
- showButton.setOnClickListener(listener);
- }
- private View.OnClickListener listener = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Button button = (Button) v;
- String filename = filenameText.getText().toString();
- switch(button.getId()){
- case R.id.button://如果是保存按钮
- int resId = R.string.success;
- String content = contentText.getText().toString();
- try {
- OutputStream outStream = DataActivity.this.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
- //四中操作模式
- //Context.MODE_PRIVATE=0 覆盖、私有
- //Context.MODE_APPEND=32768追加、私有
- //Context.MODE_WORLD_READABLE=1其他的程序可以访问
- //Context.MODE_WORLD_WRITEABLE=2
- try {
- FileService.save(outStream, content);//保存文件
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- resId = R.string.error;
- }
- } catch (FileNotFoundException e) {
- Log.e(TAG, e.toString());
- resId = R.string.error;
- }
- Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG).show();
- break;
- case R.id.showButton://如果是显示按钮
- try {
- InputStream inStream = DataActivity.this.openFileInput(filename);
- String text = FileService.read(inStream);
- resultView.setText(text);
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- resId = R.string.error;
- Toast.makeText(DataActivity.this, "读取失败", Toast.LENGTH_LONG).show();
- }
- break;
- }
- }
- };
- }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702531
0 0
- android 保存和读取文件
- Android文件保存和读取
- android 保存和读取文件
- android File文件保存和读取
- android 文件的保存和读取
- 保存和读取文件
- 文件保存和读取
- 文件保存和读取
- 文件保存和读取
- Android--文件保存与读取
- Android 文件保存与读取
- Android--文件保存与读取
- Android文件保存与读取
- Android 文件下载保存读取
- Android保存读取txt文件
- android的sdcard文件的读取和保存详细介绍
- android下pull解析器读取和保存xml文件
- android,保存文件到手机内存和读取
- Java读取txt文件,计算2011年9月份的通话时间
- 非阻塞型通信进程
- 阻塞型通信程序
- Android往SD卡上存储文件
- Android打电话&发短信
- Android文件保存和读取
- Iterator&Vector应用实例
- Linux创建线程
- Linux创建线程
- C语言计算任意数的任意次方
- 判断QQ是否在线
- Netbeans连接数据库
- 网页动态显示时间
- C语言时间函数