Android[中级教程]第二章 数据存储之File

来源:互联网 发布:保利尼奥 知乎 编辑:程序博客网 时间:2024/05/01 23:06

 接着上面一章,这次我们将数据存储在File文件里,布局文件没什么改变,还是一样的布局,看main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:layout_width="wrap_content" android:id="@+id/textView1"android:textAppearance="?android:attr/textAppearanceLarge"android:layout_height="wrap_content" android:text="TextView"></TextView><EditText android:id="@+id/editText1" android:layout_width="match_parent"android:layout_height="wrap_content" android:inputType="number"><requestFocus></requestFocus></EditText><LinearLayout android:layout_width="match_parent"android:id="@+id/linearLayout2" android:layout_height="wrap_content"><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/addOne"android:text="悟空又打死了一个妖怪"></Button><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/read_edit"android:text="读取Edit中的数据"></Button></LinearLayout><LinearLayout android:layout_width="match_parent"android:id="@+id/linearLayout1" android:layout_height="wrap_content"><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/save"android:text="存储数据"></Button><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/read"android:text="读取数据"></Button><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/clear"android:text="清除数据"></Button></LinearLayout></LinearLayout>

跟上一章的一样,也是一个EditText和几个按钮

主java文件:

import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintStream;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;import android.widget.TextView;public class Shared_PreferencesDemo extends Activity implements OnClickListener{private final String FILE_NAME = "demo.bin";private int count = 0;private String str;private TextView text;private EditText edit_text;private Button add;private Button save;private Button read;private View clear;private Button read_edit;@Overrideprotected void onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.shared_preferences);str = "悟空杀死了" + count + "只妖怪.";text = (TextView)findViewById(R.id.textView1);text.setText(str);edit_text = (EditText)findViewById(R.id.editText1);edit_text.setText(String.valueOf(count));add = (Button)findViewById(R.id.addOne);add.setOnClickListener(this);read_edit = (Button)findViewById(R.id.read_edit);read_edit.setOnClickListener(this);save = (Button)findViewById(R.id.save);save.setOnClickListener(this);read = (Button)findViewById(R.id.read);read.setOnClickListener(this);clear = (Button)findViewById(R.id.clear);clear.setOnClickListener(this);}//按钮事件监听@Overridepublic void onClick(View v){switch(v.getId()){//悟空又打死了一个妖怪按钮事件case R.id.addOne://妖怪数量加1count++;//刷新TextView和EditText控件中的值refresh();break;//读取Edit中的数据按钮事件case R.id.read_edit://取出存在SharedPreferences中的值count =Integer.parseInt(edit_text.getText().toString());refresh();break;case R.id.save://自定义写入数据方法write(String.valueOf(count));break;case R.id.read://将读取出的数据给count赋值count = Integer.parseInt(read().trim());refresh();break;case R.id.clear:count = 0;write(String.valueOf(count));refresh();break;}}/** * 从文件中读取数据,这是很简单的java应用,IO操作 * @return */private String read(){try{//打开文件输入流FileInputStream fis = openFileInput(FILE_NAME);byte[] buffer = new byte[1024];int hasRead = 0;StringBuffer sb = new StringBuffer();while((hasRead = fis.read(buffer)) > 0){sb.append(new String(buffer, 0, hasRead));}return sb.toString();} catch (Exception e){e.printStackTrace();}return null;}/** * 将数据写入文件,也是很简单的IO操作 * @param string */private void write(String string){try{//以覆盖方式打开文件输出流FileOutputStream fos = openFileOutput(FILE_NAME, 0);//将FileOutputStream包装成PrintStreamPrintStream ps = new PrintStream(fos);//输出文件内容ps.println(string);ps.close();} catch (FileNotFoundException e){e.printStackTrace();}}//自定义刷新private void refresh(){str = "悟空杀死了" + count + "只妖怪.";text.setText(str);edit_text.setText(String.valueOf(count));}}

这里面主要是文件的IO操作,也比较简单,学过JAVA的人应该知道IO操作的,具体就看代码吧,这里就不多说了,上图:

这里所不同的就是存储路径不一样,同样是存在data/data/你自己所在包文件里,但是创建在File文件夹下面的,看图:

OK,这一章也结束了,其实Android存储方式也是比较简单的,对于一些简单的数据,可以采用这两种存储方式,如果比较大型或是复杂的数据就应该采用SQLite数据库来存储了,下一章我们会介绍SQLite数据库的操作,谢谢

原创粉丝点击