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

3 篇文章 0 订阅
1 篇文章 0 订阅


布局文件

<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值