Android中文件简单存储(写入 读取)
2016-10-12 22:15
417 查看
Android中文件简单存储(写入 读取)
主要功能是从输入框中获取内容,写到文件中
MainActivity代码如下
主要功能是从输入框中获取内容,写到文件中
MainActivity代码如下
package cn.tedu.file; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener { private EditText etTitle; private EditText etContent; private Button btnSubmit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etTitle = (EditText) findViewById(R.id.et_title); etContent = (EditText) findViewById(R.id.et_content); btnSubmit = (Button) findViewById(R.id.btn_submit); btnSubmit.setOnClickListener(this); // 从文件中读取内容 FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; String title = null; String content = null; try { fis = openFileInput("riji.txt"); isr = new InputStreamReader(fis); br = new BufferedReader(isr); title = br.readLine(); content = br.readLine(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if(br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } // 将读取到的内容显示在控件上 etTitle.setText(title); etContent.setText(content); } @Override public void onClick(View v) { // 读取数据 String title = etTitle.getText().toString().trim(); String content = etContent.getText().toString(); // 数据验证 // 保存到文件 FileOutputStream fos = null; PrintWriter pw = null; try { fos = openFileOutput("riji.txt", MODE_PRIVATE); pw = new PrintWriter(fos); pw.println(title); pw.println(content); pw.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if(pw != null) { pw.close(); } } // 提示 Toast.makeText(this, "保存成功!!!", Toast.LENGTH_SHORT).show(); } }
相关文章推荐
- Android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android学习笔记五—android的存储2 文件的读取与写入
- android文件的写入与读取---简单的文本读写
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- Android对文件的操作(简单的文件读取与写入)
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput() .
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- 【Android 开发】:数据存储之File文件存储 [即SD卡的写入与读取]
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- 【Android】android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- Android文件存储(写入,读取,删除)
- 保存、方法-Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数-by小雨
- 关于android 将对象写入文件以及从文件读取对象
- FileStream 的文件读取与写入 之二(简单文件加密)