andoid存储到rom和SD卡.

//通过context的openfileoutput() 创建的文件:data/data/<包名>/files/config.txt 
//存放手机rom内存里面的.
package cn.itcast.datasave;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class DemoActivity extends Activity implements OnClickListener {
	private EditText mEtName;
	private EditText mEtPwd;
	private Button mBtLogin;
	private CheckBox mCb;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 程序开发 使用一些临时的数据 
		File cacheDir =  this.getCacheDir();
		
		
		mEtName = (EditText) this.findViewById(R.id.et_name);
		mEtPwd= (EditText) this.findViewById(R.id.et_pwd);
		mBtLogin = (Button) this.findViewById(R.id.bt_login);
		mCb = (CheckBox) this.findViewById(R.id.cb_remember_pwd);
		mBtLogin.setOnClickListener(this);
		// 默认的地址  /data/data/<包名>/
		
		//this.openFileInput可以打开一个文件输入流.输出流同理.
		//每一次初始化的时候将自动显示用户名和密码,
		//数据写到内存中
		try {
			FileInputStream  fis  = this.openFileInput("config.txt");
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len;
			while((len = fis.read(buffer))!=-1){
				bos.write(buffer, 0, len);
			}
			byte[] result = bos.toByteArray();
			String content = new String(result);
			// zhangsan: 123456
			String[] array = content.split(":");
			//把数据重新设置到文本框里面 
			mEtName.setText(array[0]);
			mEtPwd.setText(array[1]);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt_login:
			if(mCb.isChecked()){
				//保存用户名密码到持久化设备
				SavePwdService service = new SavePwdService(this);
				String name = mEtName.getText().toString().trim();
				String pwd = mEtPwd.getText().toString().trim();
				//service.saveToRomFile(name, pwd);
				
				//存到手机的sd卡里面
				service.saveToSDcard(name, pwd);
			//	Toast.makeText(this, "保存成功....", Toast.LENGTH_LONG).show();
				
			}else{
				Toast.makeText(this, "登陆中....", Toast.LENGTH_LONG).show();
			}
			
			
			break;
		}
		
	}
	
	
	
}<pre class="java" name="code">package cn.itcast.datasave;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.widget.Toast;

public class SavePwdService {
	//定义应用程序的上下文  没有被初始化 
	private Context context;
	public SavePwdService(Context context) {
		this.context = context;
	}
	/**
	 * 保存用户密码到手机rom的文件里面 
	 * @param name 
	 * @param pwd 
	 */
	public void saveToRomFile(String name ,String pwd){
		// 私有的权限 创建一个 config.txt的文件 并且获取到他的输出流 
		try {
			FileOutputStream  fos = context.openFileOutput("config.txt", Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_READABLE);
			String content = name+":"+pwd;
			fos.write(content.getBytes());
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * 把数据存到手机的外存储设备上 sd卡 
	 * @param name
	 * @param pwd
	 */
	public void saveToSDcard(String name,String pwd){
		try {
			// /sdcard/xxx.txt
			// /mnt/sdcard/xxx.txt
			// //excard/xxx.txt 	
			if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
				//若果使用外部存储的话,就需要new 出输出流的实例.
				File file = new File(Environment.getExternalStorageDirectory(),"newfile.txt");
				FileOutputStream fos = new FileOutputStream(file);
				String content = name+":"+pwd;
				fos.write(content.getBytes());
				fos.flush();
				fos.close();
				Toast.makeText(context, "保存成功", Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(context, "sd卡不可用请检查sd卡的状态", Toast.LENGTH_LONG).show();
			}

		} catch (Exception e) {
			Toast.makeText(context, "写sd卡数据失败", Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
	}
	
}

 
 


你可能感兴趣的:(andoid存储到rom和SD卡.)