Android——手机内部文件存储(作业)
作业:把用户的注册信息存储到文件里,登录成功后读出并显示出来
activity_practice2的layout文件:
<?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"
tools:context="com.hanqi.testapp3.PractiseActivity2"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_username"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_password"
android:inputType="textPassword"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="登陆"
android:onClick="bt_dl_onClick"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="注册"
android:onClick="bt_zc_onClick"
android:layout_weight="1"/>
</LinearLayout> <!--<Button-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="fdasf"-->
<!--android:onClick="bt_onClick"-->
<!--android:id="@+id/bt_1"/>-->
</LinearLayout>
activity_practice2_2的layout文件:
<?xml version="1.0" encoding="utf-8"?>
<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="com.hanqi.testapp3.PractiseActivity2_2"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名称"
android:id="@+id/et_username_1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="登陆密码"
android:id="@+id/et_password_1"
android:layout_below="@id/et_username_1"
android:inputType="textPassword"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="个性签名"
android:id="@+id/et_gxqm"
android:layout_below="@id/et_password_1"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:layout_weight="1"
android:onClick="bt_qd_onClick"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:layout_weight="1"
android:onClick="bt_qx_onClick"/>
</LinearLayout>
<!--<Button-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="fdasf"-->
<!--android:onClick="bt_onClick"-->
<!--android:id="@+id/bt_1"-->
<!--android:layout_below="@+id/et_gxqm"/>-->
</RelativeLayout>
activity_practice2_3的layout文件:
<?xml version="1.0" encoding="utf-8"?>
<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="com.hanqi.testapp3.PractiseActivity2_3"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_username"
android:textSize="30dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_gxqm"
android:layout_below="@id/tv_username"
android:textSize="30dp"/>
</RelativeLayout>
PractiseActivity2的java类:
package com.hanqi.testapp3; import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; import java.io.FileInputStream; public class PractiseActivity2 extends AppCompatActivity { EditText et_username;
EditText et_password; // Button bt_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise2);
et_username = (EditText)findViewById(R.id.et_username);
et_password = (EditText)findViewById(R.id.et_password);
//bt_1 = (Button)findViewById(R.id.bt_1);
}
public void bt_zc_onClick(View v)
{
Intent intent = new Intent(this,PractiseActivity2_2.class);
startActivity(intent);
} public void bt_dl_onClick(View v)
{
String str1 = "";
String str2 = "";
String str3 = "";
String uc = et_username.getText().toString();
String pw = et_password.getText().toString(); try {
FileInputStream fis = openFileInput("practise2-1.txt");
byte[] b = new byte[1024];
int i; while ((i=fis.read(b))>0)
{
String str = new String(b,0,i);
str1 += str;
}
fis.close(); FileInputStream fis2 = openFileInput("practise2-2.txt");
byte[] b2 = new byte[1024];
int i2 = 0;
while ((i2=fis2.read(b2))>0)
{
String str = new String(b2,0,i2);
str2 += str;
}
fis2.close(); FileInputStream fis3 = openFileInput("practise2-3.txt");
byte[] b3 = new byte[1024];
int i3 = 0;
while ((i3=fis3.read(b3))>0)
{
String str = new String(b3,0,i3);
str3 += str;
}
fis3.close();
}
catch (Exception e)
{ }
if (uc.trim().length()==0||pw.trim().length()==0)
{
Toast.makeText(PractiseActivity2.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
if (str1 ==null||(str1!=null&&!str1.equals(uc)))
{
Toast.makeText(PractiseActivity2.this, "用户未注册", Toast.LENGTH_SHORT).show();
return;
}
if (!str2.equals(pw))
{
Toast.makeText(PractiseActivity2.this, "密码错误", Toast.LENGTH_SHORT).show();
return;
}
else
{
Toast.makeText(PractiseActivity2.this, "用户验证成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,PractiseActivity2_3.class);
startActivity(intent);
finish();
}
}
// public void bt_onClick(View v)
// {
// try {
// FileInputStream fis = openFileInput("practise2-1.txt");
// byte[] b = new byte[1024];
// int i;
//
// while ((i=fis.read(b))>0)
// {
// String str = new String(b,0,i);
// str1 += str;
// }
//
// fis.close();
// }
// catch (Exception e)
// {
//
// }
// bt_1.setText(str1);
// }
}
PractiseActivity2_2的java类:
package com.hanqi.testapp3; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; import java.io.FileOutputStream;
import java.io.PrintStream; public class PractiseActivity2_2 extends AppCompatActivity { EditText et_username_1;
EditText et_password_1;
EditText et_gxqm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise2_2);
et_username_1 = (EditText)findViewById(R.id.et_username_1);
et_password_1 = (EditText)findViewById(R.id.et_password_1);
et_gxqm = (EditText)findViewById(R.id.et_gxqm);
}
public void bt_qd_onClick(View v)
{
String username = et_username_1.getText().toString();
if (username ==null||username.trim().length()==0)
{
Toast.makeText(PractiseActivity2_2.this, "请填写用户名", Toast.LENGTH_SHORT).show();
return;
}
String password = et_password_1.getText().toString();
if (password ==null||password.trim().length()==0)
{
Toast.makeText(PractiseActivity2_2.this, "请填写密码", Toast.LENGTH_SHORT).show();
return;
}
String gxqm = et_gxqm.getText().toString();
if (gxqm ==null||gxqm.trim().length()==0)
{
Toast.makeText(PractiseActivity2_2.this, "个性签名为必填内容", Toast.LENGTH_SHORT).show();
return;
}
try {
FileOutputStream fos = openFileOutput("practise2-1.txt",MODE_PRIVATE);
PrintStream ps = new PrintStream(fos);
ps.print(username);
FileOutputStream fos2 = openFileOutput("practise2-2.txt",MODE_PRIVATE);
PrintStream ps2 = new PrintStream(fos2);
ps2.print(password);
FileOutputStream fos3 = openFileOutput("practise2-3.txt",MODE_PRIVATE);
PrintStream ps3 = new PrintStream(fos3);
ps3.print(gxqm);
ps.close();
ps2.close();
ps3.close();
fos.close();
Toast.makeText(PractiseActivity2_2.this, "保存成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{ }
Intent intent = new Intent(this,PractiseActivity2.class);
startActivity(intent);
finish();
}
public void bt_qx_onClick(View v)
{
setResult(RESULT_CANCELED, null);
finish();
} }
PractiseActivity2_3的java类:
package com.hanqi.testapp3; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView; import java.io.FileInputStream; public class PractiseActivity2_3 extends AppCompatActivity { TextView tv_username;
TextView tv_gxqm; String str1 = "";
String str3 = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practise2_3);
tv_username = (TextView)findViewById(R.id.tv_username);
tv_gxqm = (TextView)findViewById(R.id.tv_gxqm); try {
FileInputStream fis = openFileInput("practise2-1.txt");
byte[] b = new byte[1024];
int i; while ((i=fis.read(b))>0)
{
String str = new String(b,0,i);
str1 += str;
} fis.close(); FileInputStream fis3 = openFileInput("practise2-3.txt");
byte[] b3 = new byte[1024];
int i3 = 0;
while ((i3=fis3.read(b3))>0)
{
String str = new String(b3,0,i3);
str3 += str;
}
fis3.close();
}
catch (Exception e)
{ }
tv_username.setText("用户名是:" + str1);
tv_gxqm.setText("个性签名是:"+str3);
}
}
效果为:










Android——手机内部文件存储(作业)的更多相关文章
- Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)
layout文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- Android入门(九)文件存储与SharedPreferences存储
原文链接:http://www.orlion.ga/578/ Android系统中主要提供了三种方式用于简单地实现数据持久化功能,即文件存储.SharedPreference存储以及数据库存储.当然, ...
- android-数据存储之手机内部file存储
一.基础概要 1.说明: 1>应用程序运行需要一些较大的数据或者图片可保存在手机内部 2>文件类型:任意 3>路径:/data/data/packageName/files/ 4&g ...
- (转)获取android手机内部存储空间和外部存储空间的参数 && 如何决定一个apk的安装位置
转:http://blog.csdn.net/zhandoushi1982/article/details/8560233 获取android文件系统的信息,需要Environment类和StatFs ...
- Android手机QQ文件夹解析
注:切勿修改手机QQ文件夹,以免造成不必要的使用问题及无法修复的数据丢失] 安卓手机QQ tencent文件夹解析 QQ下载的聊天背景:tencent→MobileQQ→system_backgrou ...
- 【Android】14.1 内部文件存储和读取
分类:C#.Android.VS2015: 创建日期:2016-02-27 一.简介 内部存储(Internal storage)是指将应用程序建立的私有文件保存在内部存储器(移动经销商卖的那种容量较 ...
- android之外部文件存储和读取
这次借用上次读写内部存储的代码,只是对将更换文件的读写路径即可.这里需要对获取SDcard的读写权限. 一.AndroidManifest.xml 这里增加了对外部存储设备的读写权限 <?xml ...
- android面试(4)---文件存储
1.sharePreference? SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数. SharedPreferences保存数据,其背后是用xml文件存放 ...
- android app 内部文件路径
public class MainActivity extends Activity { final String FILE_NAME = "crazyit.bin"; @Over ...
随机推荐
- JavaScript 获取当前时间戳
转自博客(http://blog.sina.com.cn/s/blog_8772845101019kg5.html) JavaScript 获取当前时间戳:第一种方法: var timestamp = ...
- 《BI那点儿事》数据流转换——条件性拆分
根据条件分割数据是一个在数据流中添加复杂逻辑的方法,它允许根据条件将数据输出到其他不同的路径中.例如,可以将TotalSugar< 27.4406的输出到一个路径,TotalSugar > ...
- 常用webservice接口
商业和贸易: 1.股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同时查询) Endpoint: http://webservice.webxml.com.cn/WebSe ...
- HDU-4522 湫湫系列故事——过年回家 最短路
题意:很乱 分析:把数据处理下,dijkstra下就行了,floyd超时了,我还想着优化一下输入,因为使用了vector和string等等,但是计算数据规模后,处理输入的时间复杂度比floyd要低一个 ...
- JDBC连接sql server数据库及其它
JDBC连接sql server数据库的步骤如下: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的 ...
- 无法用sysadmin权限的登录名登陆,sa密码忘了,管理员被锁在外面
作为一名DBA,你的职责就是保证数据的安全,为了达到要求,你移除了BUILTIN\Administrators组,禁用了sa登录名,修改了服务器端口,删除了所有的sysadmin权限的登录名,你可以连 ...
- Grunt自动化构建工具(网址:http://www.gruntjs.net/getting-started或者http://gruntjs.cn/getting-started)
简介:Grunt是基于Node.js的项目构建工具,对于需要重复执行的任务,例如压缩.编译.单元测试等,自动化工具可以减少你的工作量,使你的工作更轻松. 一:检测nodejs是否安装好,打开CMD控制 ...
- CentOS 6.x 一键安装PPTP VPN脚本
环境 CentOS 6.x 32位/64位XEN/KVM/OpenVZ 步骤 依次运行下列命令 #wget http://www.hi-vps.com/shell/vpn_centos6.sh #ch ...
- Linux 下 git连接github的使用
1.安装git sudo apt-get install git 2.创建github帐号 3.Linux创建SSH密钥: ssh-keygen //一直默认 4.将公钥加入到Github账户信息A ...
- spring.xml中的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...