一、权限问题


  手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据。当然,Android也为每个APP在ROM中创建一个数据存储空间,具体目录是/data/data下。在真机中调试中,该目录对于其他用户是没有开启任何权限,所以在DDMS中我们是打不开该目录的。

  解决方案就是我们通过adb登录到我们手机上,然后直接切换到root用户,这时后手机可能会询问是否授权,我们则选择允许。这样我们就成为root用户了。然后我们在更改/data目录的权限,这样就能在DDMS上看到/data下的内容了。具体操纵如下:

二、布局设置


<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="xidian.dy.com.chujia.MainActivity">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"/>
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户名和密码"
android:layout_centerVertical="true"/>
<Button
android:id="@+id/login"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录"/>
</RelativeLayout>
</LinearLayout>

三、java代码


package xidian.dy.com.chujia;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.util.PrintWriterPrinter;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.List; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.login);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText etUsername = (EditText) findViewById(R.id.username);
String username = etUsername.getText().toString();
EditText etPassword = (EditText) findViewById(R.id.password);
String password = etPassword.getText().toString();
Log.e("Login", "登录成功");
CheckBox cb = (CheckBox) findViewById(R.id.remember);
if(cb.isChecked()){
String path = "/data/data/xidian.dy.com.chujia/info.txt";
File file = new File(path);
PrintWriter pw = null;
try {
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))));
} catch (FileNotFoundException e) {
Log.e("MainActivity","file not found");
}
pw.println(username);
pw.println(password);
pw.close();
} }
});
}
}

  Android为每个app在/data/data下创建一个文件夹,文件夹的名字就是APP的包名。我们的APP报名为xidian.dy.com.chujia,所以为了保存用户数据,我在/data/data/xidian.dy.com.chujia/创建一个infro.txt文件夹,然后将用户名和密码包存到该文件夹下。对用户名和密码的保存就使用普通的java IO流即可。

三、文件之读操作


使用普通的IO流将info.txt文件中的数据读取出来,这里不牵扯读写权限问题,因为该线程的所属用户是对自己的目录进行操作的。

package xidian.dy.com.chujia;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText; import java.io.BufferedReader;
import java.io.BufferedWriter;
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.io.OutputStreamWriter;
import java.io.PrintWriter; public class MainActivity extends AppCompatActivity { private EditText etUsername;
private EditText etPassword; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.login);
etUsername = (EditText) findViewById(R.id.username);
etPassword = (EditText) findViewById(R.id.password);
if (bt != null)
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = null;
String password = null;
if (etUsername != null)
username = etUsername.getText().toString();
if (etPassword != null)
password = etPassword.getText().toString();
Log.e("Login", "登录成功");
CheckBox cb = (CheckBox) findViewById(R.id.remember);
if (cb != null && cb.isChecked()) {
File file = new File(getFilesDir(), "info.txt");
PrintWriter pw;
try {
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))));
pw.println(username);
pw.println(password);
pw.close();
} catch (FileNotFoundException e) {
Log.e("MainActivity", "file not found");
}
}
}
});
this.readAccount();
} public void readAccount() {
File file = new File(getFilesDir(), "info.txt");
if (file.exists()) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String str = br.readLine();
if (etUsername != null)
etUsername.setText(str);
str = br.readLine();
if (etPassword != null)
etPassword.setText(str);
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

android之文件存储和读取的更多相关文章

  1. Linux基础篇学习——Linux文件系统之文件存储与读取:inode,block,superblock

    Linux文件类型 代表符号 含义 - 常规文件,即file d directory,目录文件 b block device,块设备文件,支持以"block"为单位进行随机访问 c ...

  2. 【Android】14.2 外部文件存储和读取

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 1.基本概念 内部存储的私有可用存储空间一般都不会很大,对于容量比较大的文件,例如视频等,应该将其存储在外部存储设 ...

  3. 【Android】14.1 内部文件存储和读取

    分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...

  4. 19.Android之文件存储方法学习

    Android开发中会用到文件存储,今天来学习下. 先改下布局界面: <?xml version="1.0" encoding="utf-8"?> ...

  5. Android学习——文件存储

    在Andriod开发中,文件存储和Java的文件存储类似.但需要注意的是,为了防止产生碎片垃圾,在创建文件时,要尽量使用系统给出的函数进行创建,这样当APP被卸载后,系统可以将这些文件统一删除掉.获取 ...

  6. Android File文件存储功能

    1.介绍 2.使用方法 3.文件存储位置 4.java后台代码 package com.lucky.test47file; import android.support.v7.app.AppCompa ...

  7. android 开发-文件存储之读写sdcard

    android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...

  8. Android使用文件存储数据

    Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...

  9. Android 使用SQLite存储以及读取Drawable对象

    在进行Android开发过程中,我们经常会接触到Drawable对象,那么,若要使用数据库来进行存储及读取,该如何实现? 一.存储 //第一步,将Drawable对象转化为Bitmap对象 Bitma ...

随机推荐

  1. 07_旅行商问题(TSP问题,货郎担问题,经典NPC难题)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P61 问题9: 问题描述:有n(n<=15)个城市,两两之间均有道路直接相连,给出每两个城市i和j之间的道路长度L[i][j],求 ...

  2. I Hate It(线段数组基础题)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. php类型转换以及类型转换的判别

    部分摘自PHP: 类型 - Manual 相关链接 PHP 在变量定义中不需要(或不支持)明确的类型定义:变量类型是根据使用该变量的上下文所决定的.也就是说,如果把一个 string 值赋给变量 $v ...

  4. Mongodb 主从复制与副本集实验

    1.实验主从复制,并验证复制成功,抓图实验过程  Step1:创建相应的目录 Mkdir -p ./dbs/master Mkdir -p ./dbs/slave Step2:开启主服务 ./bin/ ...

  5. 如何用dos命令运行testng

    写好的自动化程序怎么让它运行呢,总不能每次都启动eclipse吧,下面就先介绍一种用dos命令运行testNG的方法. 1.把项目打成jar吧,我用的是Fat jar工具. 2.在电脑的某个盘建一个文 ...

  6. 转载:HttpClient使用详解

    原文地址:http://blog.csdn.net/wangpeng047/article/details/19624529 Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自 ...

  7. J2EE笔记3

    7. MVC 设计模式. 6. 和属性相关的方法: 1). 方法 void setAttribute(String name, Object o): 设置属性 Object getAttribute( ...

  8. 使用Android Studio搭建Android集成开发环境(图文教程)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  9. 如何修改myeclipse 内存,eclipse.ini中各个参数的作用。

    修改MyEclipse/eclipse文件夹中配置文件eclipse.ini中的内存分配就哦了 =================================== 一般的ini文件设置主要包括以下 ...

  10. The Geometry has no Z values 解决办法

    from:http://dufan20086.blog.163.com/blog/static/6616452320145269343675/ 我们在创建要素时,简单的IFeatureClass.Cr ...