Android入门笔记24 内部存储

  Android系统允许应用程序创建仅能够自身访问的私有文
件,文件保存在设备的内部存储器上的/data/data/<package name>/files目录中。和传统的   

     Java中实现I/O的程序类似,在Android中,其提供了openFileInput 和 openFileOutput 方法读取设备上的文件。      

  (1).openFileOutput(String name, int mode)     //String 为文件名、第二个参数为模式 

          openFileOutput()方法打开的应用程序私有文件,为写入数据做准备。在默认情    况下,写入的时候会  覆盖原文件内容,如果想把新写入的内容附加到原文件内容后,则可以指定其mode为  Context.MODE_APPEND;如果指定的打开文件不存在时,则创建一个新的文件。默认 情况下使用openFileOutput方法创建的文件只能被其调用的应用使用,其他应用无法读取这个文件。 
     注意: 

      为了提高文件系统的性能,一般调用write()函数时,如果写入的数据量较小,系统会把数据保存在数     据缓冲区中,等数据量累积到一定程度时再一次性地写入文件中。由此可知,在调用close()函数关闭文件前,务必要调用flush()函数,将缓冲区内所有的数据写入文件。 


       (2).openFileIntput(String name, int mode) 

    //String 为文件名、第二个参数为模式

 

     安卓系统提供的四种模式

模式

说明

MODE_PRIVATE

私有模式,缺陷模式,文件仅能够被文件创建程序访问,或具有相同UID的程序访问

MODE_APPEND

追加模式,如果文件已经存在,则在文件的结尾处添加新数据

MODE_WORLD_READABLE

全局读模式,允许任何程序读取私有文件

MODE_WORLD_WRITEABLE

全局写模式,允许任何程序写入私有文件




 

package com.example.l014internalstorage;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private EditText editText;
	private Button savaBtn,readBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		editText = (EditText) findViewById(R.id.editText);
		savaBtn = (Button) findViewById(R.id.savebtn);
		savaBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				saveCurrentText();	
			}
		});
		
		readBtn = (Button) findViewById(R.id.readbtn);
		readBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				readSavedText();
			}
		});
		
	}

	private void saveCurrentText()   {
		try {
			OutputStream out = openFileOutput("data", Context.MODE_PRIVATE);
			out.write(editText.getText().toString().getBytes("utf-8"));
			out.flush(); //从内存写入到文件中
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
	
	private void readSavedText()
	{
		try {
			InputStream in = openFileInput("data");
			byte[] bytes = new byte[in.available()];
			in.read(bytes);
			String str = new String(bytes,"utf-8");
			editText.setText(str);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值