Android存储数据到本地文件

xml文件

<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"
    android:padding="5dp"
 >

    <EditText
        android:id="@+id/qqnum"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       	android:hint="请输入QQ"
       	android:inputType="number"
        android:textSize="20dp"
        />
    <EditText 
        android:id="@+id/pass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword"
        android:textSize="20dp"
        
        />
    <CheckBox 
        android:id="@+id/rem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        
        />
    <Button 
        android:id="@+id/Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"/>

</LinearLayout>

Utils

package com.example.android22filelogin;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.text.TextUtils;

public class Utils {
	public static boolean saveUserInfo(String username,String pwd){
		String data=username+"##"+pwd;
		String path="/data/data/com.example.android22filelogin/data.txt";
		try {
			FileOutputStream out=new FileOutputStream(path);
			out.write(data.getBytes());
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public static Map<String,String> getUserInfo(){
		String path="/data/data/com.example.android22filelogin/data.txt";
		try {
			BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
			String data=reader.readLine();
			if(!TextUtils.isEmpty(data))
			{
				String [] datas=data.split("##");
				Map<String, String> userinfo=new HashMap<String, String>();
				userinfo.put("number", datas[0]);
				userinfo.put("pwd", datas[1]);
				return userinfo;

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}

activity

public class MainActivity extends Activity implements OnClickListener {
	private EditText qqnum,pwd;
	private CheckBox rem;
	private Button but;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        qqnum=(EditText)findViewById(R.id.qqnum);
        pwd=(EditText)findViewById(R.id.pass);
        rem=(CheckBox)findViewById(R.id.rem);
        but=(Button)findViewById(R.id.Login);
        but.setOnClickListener(this);
        //回显数据
        Map<String, String> userinfo=Utils.getUserInfo();
        if(userinfo!=null)
        {
        	qqnum.setText(userinfo.get("number"));
        	pwd.setText(userinfo.get("pwd"));
        }
    }

	@Override
	public void onClick(View v) {
		//记住号码和密码

		String num=qqnum.getText().toString();
		String password=pwd.getText().toString();
		if(TextUtils.isEmpty(num)||TextUtils.isEmpty(password))
		{
			Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
			return;
		}
		//判断是否记住密码
		if(rem.isChecked())
		{
			boolean isSuccess=Utils.saveUserInfo(num, password);
			Toast.makeText(this, isSuccess+"", Toast.LENGTH_LONG).show();
		}
		//登录成功

	}

}
时间: 2024-08-05 23:10:47

Android存储数据到本地文件的相关文章

Android存储和加载本地文件(外部存储设备)

有时候应用需要将数据写入到设备的外部存储上.列如,需要同其他应用或用户共享音乐.图片或者网络下载资料时,保存在外部设备的数据共享起来要比较方便.而且,外部设备通常具有更大的存储空间. 我们可以通过android.os.Environment.getExternalStorageDirectory()方法获取sdCard的路径.再在此路径下创建一个MyFiles的文件,将数据保存在MyFiles文件夹下. 下面就展示如何在外部存储设备中存储和加载本地文件: 1.创建一个名为 DataStorage

存储和加载本地文件(内部存储设备)

Android设备上的所有应用都有一个放置在沙盘中的文件目录,将文件保存到沙盒中可以阻止其他应用的访问. 沙盒目录的全路径为:/data/data/<包名>  用File Explorer查看: 如上图可见,每个应用都在/data/data下有一个以此应用包名命名的文件目录. 而本文就是介绍将文件保存在/data/data/<包名>/files/ 目录下 下面就展示如何在内部存储设备中存储和加载本地文件: 1.创建一个名为 DataStorage的工程 2.准备好布局文件(acti

Android 添加数据到本地Excel表中

由于项目需要,今天学习了一下如何将程序里的数据添加到本地的Excel表中. 下面为学习笔记: 先上效果图: 首先,需要导入jxl.jar包到libs文件夹内. 然后创建Excel表,并往表里添加表头. // 创建excel表. public void createExcel(File file) { WritableSheet ws = null; try { if (!file.exists()) { // 创建表 wwb = Workbook.createWorkbook(file); //

android存取数据方式:文件、SharedPreferences

android存取数据方式:文件.SharedPreferences.SQLite 数据库.Content provider 文件流: 使用java IO流对文件进行读写操作,文件权限默认. 指定文件权限写入:mode:文件权限MODE_PRIVATE的文件是应用程序私有的,MODE_WORLD_READABLE则所有应用程序都可以访问的, MODE_WORLD_WRITEABLE所有应用程序都可以写,mode_APPEND则是如果要创建的文件存在则新写入的数据不会覆盖以前的数据. openFi

Android存储数据方式(转)

转自http://www.cnblogs.com/hanyonglu/archive/2012/03/01/2374894.html   本文介绍Android中的5种数据存储方式. 数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用ContentProvider存储数据 5 网络存储数据 下面将为大家一一详细介绍. 第一种: 使用Sha

Android存储数据的8种方式

第一种:SharedPreferences 1.使用键值对的方式存储数据 2.通常用于:保存用户的偏好设置.选择是否保存密码.记录文档阅读的位置等 3.实现方式(写入): a).获取SharedPreferences对象     getSharedPreferences("文件名",MODE_PRIVATE/MODE_MULTI_PROCESS) 前者表示只能当前应用程序访问该文件:后者表示可以多个进程访问该文件:文件是存放在:data/data/<package name>

NSUserDefaults 存储数据到本地,NSUserDefaults 使用

//以下是我用 NSUserDefaults 将 NSData 类型的地址数组存在本地. UserAddressEntity *userAddressEntity = [[UserAddressEntity alloc]init];     userAddressEntity.addName = addName.text;     userAddressEntity.addPhone = addPhone.text;     userAddressEntity.addressInfo = [NS

(尚030)Vue_案例_存储数据(localStorage本地存储技术)

当刷新页面时,会变为原来的状态 1.问题:当我刷新是不希望改变原来状态 需要缓存我当前的数据 2.究竟是缓存在内存里还是在文件里??? 缓存在文件里,因为浏览器关闭了,内存就没了;而我们需要重新打开浏览器状态还在 可以用localStorage这种本地技术(本质是缓存在文件里) 3.存储带来的问题 什么时候存?什么时候读?存啥东西进去? 界面显示是根据todos显示的,所以需要将todos存起来; 我随便做任何操作后,都是我最后的状态,也就是只要我的界面发生改变就需要存(todos改变就要存)

hive导出数据到本地文件报错解决方法

hive导出到本地数据报错 Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.MoveTask. Unable to move source hdfs://localhost:9000/tmp/hive/daisy/185ccfc8-52f0-48e4-acd2-866340445241/hive_2020-01-21_11-00-58_110_6359830348207520702-1/-mr-10000 to