安卓数据读写
通过文件操作和数据库操作进行数据读写
1、完成安卓对文件的读写操作。分成读写不一致和一致两种情况。
2、完成安卓对SQLite数据库的操作。包括数据库建立、创建表格、插入数据、更新数据、删除数据以及查询数据。
附加题:对SQLite的所有操作都是根据用户的输入完成。
一、安卓对文件的读写
首先布局如上,分别初始化两个文本和两个按钮,并添加监听事件。文件读写可以分别用两个try捕获异常
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText=(EditText)findViewById(R.id.editText_input);
Button buttonSave=(Button)findViewById(R.id.button_save);
final TextView textView=(TextView)findViewById(R.id.textView_read);
Button buttonRead=(Button)findViewById(R.id.button_read);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
FileOutputStream fos=openFileOutput("20210406",MODE_PRIVATE);
fos.write(editText.getText().toString().getBytes());
fos.close();
Toast.makeText(MainActivity.this,"file saved",Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
});
buttonRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
FileInputStream fis=