Android数据存储内置存储的使用

来源:互联网 发布:centos 锁屏 编辑:程序博客网 时间:2024/05/29 12:04

实现往内置存储中写入文件,读取文件,删除文件:

      核心代码示例如下:

             1)内部存储的写入:

                 方法一:FileOutputStream  fileOutputStream = openFileOutput(String name,int mode);

                 方法二:File file = new File(getFilesDir(), 文件名称);

                             FileOutputStream out = new  FileOutputStream(file);

             2)内部存储的读取:

                 方法一:FileInputStream in = openFileInput(String name);

                 方法二:File file = new File(getFilesDir(), 文件名称);

                            FileInputStream in = newFileInputStream(file);

              3)内部存储的删除:

                            deleteFile();

完整Demo示例如下:

XML布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin" >    <LinearLayout        android:paddingLeft="@dimen/activity_horizontal_margin"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <EditText            android:id="@+id/et_name"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@android:drawable/editbox_background"            android:hint="请输入文件名"            android:singleLine="true" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@android:drawable/edit_text"            android:onClick="saveFile"            android:text="保存" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@android:drawable/edit_text"            android:onClick="openFile"            android:text="打开" />    </LinearLayout>    <EditText        android:id="@+id/et_content"        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:background="@android:drawable/edit_text"        android:hint="文件内容" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@android:drawable/edit_text"        android:onClick="deleteFile"        android:text="删除文件" /></LinearLayout>
Java代码:

public class MainActivity extends Activity {   /**    * 文件存储的位置:    * data/data/包名/files/    */   private Button save, open, delete;   private EditText fileName, fileContent;   @Override   protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);      // 初始化控件      initView();   }   // 保存按钮   public void saveFile(View v) {      /      if (TextUtils.isEmpty(fileContent.getText().toString())                 && TextUtils.isEmpty(fileName.getText().toString())) {         return;      }      try {         FileOutputStream fileOutputStream =                openFileOutput(fileName.getText().toString(), MODE_PRIVATE|MODE_APPEND);         fileOutputStream.write(fileContent.getText().toString().getBytes());         fileOutputStream.flush();         fileOutputStream.close();         fileName.setText("");         fileContent.setText("");         Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_LONG).show();      } catch (Exception e) {         // TODO Auto-generated catch block         e.printStackTrace();      }   }     //读取内置存储中的文件   public void openFile(View view){      ByteArrayOutputStream outputStream = null;      try {         FileInputStream inputStream = new FileInputStream(new File(getFilesDir(),fileName.getText().toString()));               outputStream = new ByteArrayOutputStream();         byte [] buffer = new byte [8*1024];         int len=0;         while ((len = inputStream.read(buffer))!=-1) {             outputStream.write(buffer, 0, len);             outputStream.flush();         }                String string = new String(outputStream.toByteArray());         fileContent.setText(string);        } catch (Exception e) {         e.printStackTrace();      }finally{                  if (outputStream != null) {            try {               outputStream.close();            } catch (IOException e) {               e.printStackTrace();            }         }              }   }   //删除按钮   public void deleteFile(View view){      boolean deleteFile = deleteFile(fileName.getText().toString());            if (deleteFile) {         Toast.makeText(this, "删除成功!!", Toast.LENGTH_LONG).show();      }       }     private void initView() {      fileName = (EditText) findViewById(R.id.et_name);      fileContent = (EditText) findViewById(R.id.et_content);   }}

1 0
原创粉丝点击