Files存储练习
Posted 一个不愿透漏姓名的朝阳群众叫旺仔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Files存储练习相关的知识,希望对你有一定的参考价值。
package com.hanqi.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.PrintStream; public class Main2Activity extends AppCompatActivity { EditText user_name; EditText user_password; Button bt_1; Button bt_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); user_name=(EditText)findViewById(R.id.user_name); user_password=(EditText)findViewById(R.id.user_password); bt_1=(Button)findViewById(R.id.bt_1); bt_1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String input_username=user_name.getText().toString(); String input_password=user_password.getText().toString(); if (input_username.trim().length()==0||input_password.trim().length()==0) { Toast.makeText(Main2Activity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show(); }else { Intent intent=new Intent(Main2Activity.this,Main22Activity.class); startActivity(intent); try { File file=getFilesDir(); String path=file.getAbsolutePath(); FileOutputStream fos=openFileOutput("test.txt", MODE_APPEND); PrintStream ps=new PrintStream(fos); ps.print(input_username); ps.close(); fos.close(); FileOutputStream fos1=openFileOutput("tes.txt", MODE_APPEND); PrintStream ps1=new PrintStream(fos1); ps1.print(input_password); ps1.close(); fos1.close(); Toast.makeText(Main2Activity.this, "保存成功", Toast.LENGTH_SHORT).show(); }catch (Exception e) { e.printStackTrace(); } } } }); } }
package com.hanqi.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.EditText; import java.io.FileInputStream; public class Main22Activity extends AppCompatActivity { EditText file_name; EditText file_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main22); file_name=(EditText)findViewById(R.id.file_name); file_password=(EditText)findViewById(R.id.file_password); try { FileInputStream fis =openFileInput("test.txt"); byte[] bytes =new byte[1024]; int i=0; String str1 = ""; while ((i=fis.read(bytes))>0) { String str = new String(bytes,0,i); str1+=str; } fis.close(); file_name.setText(str1); int ii=0; FileInputStream fis1 =openFileInput("tes.txt"); byte[] bytes1 =new byte[1024]; String str2 = ""; while ((ii=fis1.read(bytes1))>0) { String str = new String(bytes1,0,ii); str2+=str; } fis1.close(); file_password.setText(str2); }catch (Exception e) { e.printStackTrace(); } } }
<?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.myapplication.Main2Activity" android:orientation="vertical"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/user_name" android:hint="用户名"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/user_password" android:hint="用户密码"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_1" android:text="登陆" /> </LinearLayout>
<?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.myapplication.Main22Activity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <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/file_name"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <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/file_password"/> </LinearLayout> </LinearLayout>
以上是关于Files存储练习的主要内容,如果未能解决你的问题,请参考以下文章
Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决(代码片段
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段