android之文件存储和读取
一、权限问题
手机中存储空间分为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之文件存储和读取的更多相关文章
- Linux基础篇学习——Linux文件系统之文件存储与读取:inode,block,superblock
Linux文件类型 代表符号 含义 - 常规文件,即file d directory,目录文件 b block device,块设备文件,支持以"block"为单位进行随机访问 c ...
- 【Android】14.2 外部文件存储和读取
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 1.基本概念 内部存储的私有可用存储空间一般都不会很大,对于容量比较大的文件,例如视频等,应该将其存储在外部存储设 ...
- 【Android】14.1 内部文件存储和读取
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...
- 19.Android之文件存储方法学习
Android开发中会用到文件存储,今天来学习下. 先改下布局界面: <?xml version="1.0" encoding="utf-8"?> ...
- Android学习——文件存储
在Andriod开发中,文件存储和Java的文件存储类似.但需要注意的是,为了防止产生碎片垃圾,在创建文件时,要尽量使用系统给出的函数进行创建,这样当APP被卸载后,系统可以将这些文件统一删除掉.获取 ...
- Android File文件存储功能
1.介绍 2.使用方法 3.文件存储位置 4.java后台代码 package com.lucky.test47file; import android.support.v7.app.AppCompa ...
- android 开发-文件存储之读写sdcard
android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...
- Android使用文件存储数据
Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...
- Android 使用SQLite存储以及读取Drawable对象
在进行Android开发过程中,我们经常会接触到Drawable对象,那么,若要使用数据库来进行存储及读取,该如何实现? 一.存储 //第一步,将Drawable对象转化为Bitmap对象 Bitma ...
随机推荐
- Bash Shell read file line by line and substring
#read one file line by line for line in $(cat test1.txt); do echo $line ; done; #while read split li ...
- OpenStack overview 笔记
Example architecture example architecture 至少需要两个节点启动一个虚拟机或者实例.可选的服务,例如Block storage和Object storage需要 ...
- 分享用于学习C++图像处理的代码示例
为了便于学习图像处理并研究图像算法, 俺写了一个适合初学者学习的小小框架. 麻雀虽小五脏俱全. 采用的加解码库:stb_image 官方:http://nothings.org/ stb_image. ...
- 八皇后,回溯与递归(Python实现)
八皇后问题是十九世纪著名的数学家高斯1850年提出 .以下为python语句的八皇后代码,摘自<Python基础教程>,代码相对于其他语言,来得短小且一次性可以打印出92种结果.同时可以扩 ...
- POJ 3233 Matrix Power Series --二分求矩阵等比数列和
题意:求S(k) = A+A^2+...+A^k. 解法:二分即可. if(k为奇) S(k) = S(k-1)+A^k else S(k) = S(k/2)*(I+A^(k/2)) ...
- HDU 1892 See you~
最裸的二维树状数组,但是因为内存太大(c[1010][1010]),好像不能运行,结果蒙着写,写了好久.. 代码: #include <iostream> #include <cst ...
- sql 入门经典(第五版) Ryan Stephens 学习笔记 (第六,七,八,九,十章,十一章,十二章)
第六章: 管理数据库事务 事务 是 由第五章 数据操作语言完成的 DML ,是对数据库锁做的一个操作或者修改. 所有事务都有开始和结束 事务可以被保存和撤销 如果事务在中途失败,事务中的任何部分都不 ...
- Spring AOP 针对注解的AOP
我也忘记是从哪里扒来的代码,不过有了这个思路,以后可以自己针对 Controller 还有 Service层的任意 方法进行代理了 package pw.jonwinters.aop; import ...
- C# 小型资源管理器
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 浅析C#深拷贝与浅拷贝(转)
1.深拷贝与浅拷贝 拷贝即是通常所说的复制(Copy)或克隆(Clone),对象的拷贝也就是从现有对象复制一个“一模一样”的新对象出来.虽然都是复制对象,但是不同的 复制方法,复制出来的新对象却并 ...