Android保存文件到手机

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="26dp"
        android:text="请输入用户名:" />

    <EditText
        android:id="@+id/et_userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="29dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_userName"
        android:layout_below="@+id/et_userName"
        android:layout_marginTop="35dp"
        android:text="请输入密码:" />

    <EditText
        android:id="@+id/et_pwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="56dp"
        android:ems="10"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_pwd"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        android:text="记住密码" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/et_pwd"
        android:layout_alignTop="@+id/checkBox1"
        android:text="登录"
        android:onClick="login" />

</RelativeLayout>


MainActivity.java

package com.demo.iotest;

import java.io.IOException;
import java.util.Map;

import com.demo.loginService.LoginService;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";
	private EditText et_userName;
	private EditText et_pwd;
	private CheckBox checkBox;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et_userName = (EditText) findViewById(R.id.et_userName);
		et_pwd = (EditText)findViewById(R.id.et_pwd);
		checkBox = (CheckBox) findViewById(R.id.checkBox1);
		
		//检查是否有保存的用户名和密码数组,如果有,回显
		Map<String, String> map;
		try {
			map = LoginService.getSavedUserInfo(this);
			if(map != null){
				et_userName.setText(map.get("userName"));
				et_pwd.setText(map.get("pwd"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

	}

	public void login(View view){
		String userName = et_userName.getText().toString().trim();	
		String pwd = et_pwd.getText().toString().trim();	
		if(TextUtils.isEmpty(userName) || TextUtils.isEmpty(pwd)){
			Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_SHORT).show();
		}else{
			//登陆
			//判断是否保存密码
			if(checkBox.isChecked()){
				//保存用户名密码
				Log.i(TAG, "保存用户名密码");
				try {
					boolean result = LoginService.saveUserInfo(this,userName, pwd);
					if(result){
						Toast.makeText(this, "保存用户信息成功", 0).show();
					}else{
						Toast.makeText(this, "保存用户信息失败", 0).show();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			//登陆,发送消息到服务器,服务器验证是否正确
			if("zhangsan".equals(userName) && "123".equals(pwd)){
				Toast.makeText(this, "登录成功", 0).show();
			}else{
				Toast.makeText(this, "登录失败,用户名或者密码错误", 0).show();
			}
		}
	}
}


LoginService.java


package com.demo.loginService;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;

public class LoginService {
	/**
	 * 保存用户名密码的业务方法
	 * @param userName 用户名
	 * @param context 上下文
	 * @param pwd 密码
	 * @return true 保存成功 false 保存失败
	 * @throws IOException 
	 * 
	 * getFileDir  /data/data/包名/files
	 * getCacheDir  /data/data/包名/cache
	 */
	public static boolean saveUserInfo(Context context,String userName,String pwd) throws IOException{
		try {
//			context.getFilesDir();	//帮助返回一个目录/data/data/报名/files
//			File file = new File("/data/data/com.demo.iotest/info.txt");
			File file = new File(context.getFilesDir(),"info.txt");
			FileOutputStream fos = new FileOutputStream(file);
			fos.write((userName + "##" + pwd).getBytes());
			fos.close();
			return true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} 
		
		
	}
	public static Map<String,String> getSavedUserInfo(Context context) throws Exception{
		
		File file = new File(context.getFilesDir(),"info.txt");
		try {
			FileInputStream fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			String str = br.readLine();
			String[] info = str.split("##");
			Map<String, String> map = new HashMap<String, String>();
			map.put("userName", info[0]);
			map.put("pwd", info[1]);
			return map;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	
	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值