安卓中InternalStorage内存存储的几种方式

来源:互联网 发布:解题软件哪个好 编辑:程序博客网 时间:2024/06/05 12:03


布局文件

<span style="font-size:18px;"><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" >    <EditText        android:id="@+id/et"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入内容" />    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/wr1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="写入数据1" />        <Button            android:id="@+id/rd1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="读出数据1" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/wr2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="写入数据2" />        <Button            android:id="@+id/rd2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="读出数据2" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/wr3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="写入数据3" />        <Button            android:id="@+id/rd3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="读出数据3" />    </LinearLayout>    <Button            android:id="@+id/delete"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="click"            android:text="删除数据" /></LinearLayout></span>

逻辑代码文件:
<span style="font-size:18px;">package com.example.day13_internalstorage;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {EditText et;TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        et=(EditText) findViewById(R.id.et);        tv=(TextView) findViewById(R.id.tv);    }    public void click(View v)    {    switch(v.getId())    {    case R.id.wr1:    {    try         {    OutputStream out=openFileOutput("myfile",MODE_PRIVATE);    out.write(et.getText().toString().getBytes());    out.flush();    out.close();        }         catch (Exception e)         {    e.printStackTrace();    }    }    break;    case R.id.rd1:    {    StringBuffer sb=new StringBuffer();        try         {    InputStream in=openFileInput("myfile");    BufferedReader br=new BufferedReader(new InputStreamReader(in));    String count = null;    while((count=br.readLine())!=null)    {    sb.append(count);    }    tv.setText(sb.toString());    br.close();        }         catch (Exception e)         {    e.printStackTrace();    }    }    break;    case R.id.wr2:    {    try     {        File file=new File(getFilesDir(), "myfile.txt");FileOutputStream fo=new FileOutputStream(file);fo.write(et.getText().toString().getBytes());fo.flush();fo.close();    }     catch (Exception e)     {e.printStackTrace();}        }    break;    case R.id.rd2:    {    try     {        File file=new File(getFilesDir(), "myfile.txt");FileInputStream fo=new FileInputStream(file);int count=0;byte b[]=new byte[1024];ByteArrayOutputStream bo=new ByteArrayOutputStream();while((count=fo.read(b))!=-1){bo.write(b,0, count);bo.flush();}tv.setText(bo.toString());fo.close();    }     catch (Exception e)     {e.printStackTrace();}    }    break;    case R.id.wr3:    {    try     {    //文件名,文件后缀名,文件保存路径        File file=File.createTempFile("temp",null,getCacheDir());        FileOutputStream fo=new FileOutputStream(file);        fo.write(et.getText().toString().getBytes());        fo.close();    }     catch (Exception e)     {e.printStackTrace();}    }    break;    case R.id.rd3:    {    try {    //缓冲文件每次保存时文件名不同,需回来找文件名        File file=new File(getCacheDir(), "temp-117631436.tmp");FileInputStream fi=new FileInputStream(file);InputStreamReader ir=new InputStreamReader(fi);BufferedReader br=new BufferedReader(ir);StringBuffer sb=new StringBuffer();String line=null;while((line=br.readLine())!=null){sb.append(line);}tv.setText(sb.toString());//br.close();    }     catch (Exception e)     {e.printStackTrace();}    }    break;    case R.id.delete:    {    boolean b=deleteFile("myfile.txt");    if(b)    {    Toast.makeText(getApplicationContext(), "删除成功", 0).show();    }else    {    Toast.makeText(getApplicationContext(), "删除失败", 0).show();    }    }    break;        }         }     }</span>


0 0