扩展:
- final 变量:在使用的时候,是直接被替换成他的值
- String 变量:在使用的时候是链接对象地址使用的
- 在使用Java局部内部类或者内部类时,若该类调用了所在方法的局部变量,则该局部变量必须使用final关键字来修饰,
否则将会出现编译错误“Cannot refer to a non-final variable * inside an inner class defined in a different method” - 相对路径:…/就是向上走一层
Android中的存储种类:sharedPreferences、内部存储、外部存储、Sqlite数据库
一.sharedPreferences存储
- sharedPreferences
1.1 获取sharedPreferences对象 * 函数getSharePreferences(String fileName,int mode) (fileName是最终要存的文件名;mode权限一般就是一个安卓常量) mode里面有内容参数: * MODE_PRIVATE:仅供自己的app使用 * MODE_MULTI_PROCESS:多个app可读,跨应用 * MODE_APPEND:追加内容,不存在则创建在写入 * 函数getPreferences(int mode) (不常用,只读文件。eg:sharedPreferences mysp=getshsharedPreferences()) 1.2 获取sharedPreferences子对象 SharePreferences.Editor editor=mysp.edit(); 1.3 向editor添加数据 editor.putString(key,value) editor.putInt(key,value) editor.putBoolean(key,value) 1.4 提交数据 editor.commit();
- Eg:示例(账号,密码保存,点击注册,实现保存)
final EditText username=(EditText) findViewById(R.id.username);//注意这里要在下面方法中使用,所以定义为final变量,具体原因,见上面的扩展*** final EditText password=(EditText) findViewById(R.id.password); Button regist=(Button) findViewById(R.id.regist); Button login=(Button) findViewById(R.id.login); //1.准备写入对象 final SharedPreferences mysp = getSharedPreferences("passport", MODE_PRIVATE); //在点击事件之前就已经连接到了SharedPreferences数据库,并设置了相应的文件名和使用权限模式 //2.写入 regist.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { //2.1获得数据 String input_username=username.getText().toString(); String input_password=password.getText().toString(); //2.2获得可编写对象 SharedPreferences.Editor editor=mysp.edit(); //这里获得了数据库的编写权限,就可以在下面编写了 //2.3放入数据 editor.putString("username", input_username); //这里通过编写权限声明的对象,在数据库中放入内容 editor.putString("password", input_password); //2.4提交 editor.commit(); //注意最后一定要提交 Toast.makeText(getApplicationContext(), "注册成功", 2).show(); } });
- 查找保存的数据在哪里
window-show view-other-android-file explorer-data-data-想要找的包的名称-shared prefs-存的文件-点击右上角红色按钮(pull a file from the device)-找到导出文件用记事本打开 上面例子中显示的数据如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="username">zhangsan</string> //我输入的账号名 <string name="password">zhangsan</string> //我输入的密码 </map>
- 读取sharedPreferences
mysp.getString(value,null) //第一个参数为:要从数据库中提取信息的名字,第二个参数是:如果没有信息要输出的备用值,可以设空 然后用if来验证,输入值和数据库中存在的值是否相匹配,如果匹配,就完成相应的操作,不匹配else,完成相应的操作 具体登录例子如下: //读取数据,点击登录验证 login.setOnClickListener(new OnClickListener() { //设置登录按钮点击事件 public void onClick(View arg0) { //获得填入的账号密码 String input_username=username.getText().toString(); String input_password=password.getText().toString(); //读取数据库的账号和密码 String exis_username=mysp.getString("username", null); //这里用到了getString(),与上面的putString)()相对于应 String exis_password=mysp.getString("password", null); //验证 if(input_username.equals(exis_username)&&input_password.equals(exis_password)){//if判断在这里 Intent intent=new Intent(MainActivity.this,Info.class);//这里设置了一个跳转界面 Toast.makeText(getApplicationContext(), "登录成功", 2).show(); } else{ Toast.makeText(getApplicationContext(), "账号或密码错误", 2).show(); } } });
- 注册设置,如果账号相同就显示,账号已存在
- 记住密码
二.内部存储 I/O流:
内部存储 I/O流特点:
- 只能被创建它的应用访问
- 卸载app后就没了
- 避免使用,内存满了就GG了
输出流:openFileOutput(filename,mode) //打开文件输出流,并规定模式
输入流:openFileInput(filename) //打开文件输入流,读取
getDir(name,mode) //在app的data目录下获取创建的name的子目录
getFileDir //获取文件的绝对路径
String[] 变量 =fileList(); //返回app的data下的全部文件
deleteFile(filename) //删除文件
记事本示例:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button savaButton=(Button)findViewById(R.id.save);
savaButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EditText text=(EditText)findViewById(R.id.text);//获取输入框的文本
String textString=text.getText().toString();
//创建一个 文件,用来写入
FileOutputStream file=null;//先准备一个输出文件的对象
try {
file=openFileOutput("first", MODE_PRIVATE);//对象=一个文件,打开文件
try {
file.write(textString.getBytes());//根据函数参数的类型,写入内容
file.flush();//清楚缓存
Toast.makeText(getApplicationContext(), "保存成功", 2).show();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
//退出
Button quit=(Button)findViewById(R.id.quit);
quit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
//读取
Button read=(Button) findViewById(R.id.read);
read.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try {
FileInputStream readFile=openFileInput("first");//准备文件输入流对象
try {
byte [] buffer=new byte[readFile.available()];//实例化字节数组
readFile.read(buffer);//读取
readFile.close();//关闭
String data=new String(buffer);
EditText textEdit=(EditText)findViewById(R.id.text);
textEdit.setText(data);
System.out.println(data.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
//获取文件列表
String [] fileList=fileList();
for(int i=0;i<fileList.length;i++){
System.out.println(fileList[i]);
}
//获取绝对路径
System.out.println("绝对路径"+getFilesDir());
}
三.外部存储
外部存储不单单是指SD卡,一般是指连到电脑能被识别的,就是外部存储。
sava后文件存在data文件夹-media文件夹-0文件夹-mytext(这个是文档名,可以改变)
注意:在配置文件中需要限制读写权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
记事本示例:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//-----------------------------------------------------------区别1----------------------------------------------------------------
//实例化外部文件对象
final File waitFile=new File(Environment.getExternalStorageDirectory(),"mytext");
Button savaButton=(Button)findViewById(R.id.save);
savaButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EditText text=(EditText)findViewById(R.id.text);//获取输入框的文本
String textString=text.getText().toString();
//创建一个 文件,用来写入
FileOutputStream file=null;//先准备一个输出文件的对象
try {
//-----------------------------------------------------------区别2-----------------------------------------------------
file=new FileOutputStream(waitFile);//对象=一个文件,打开文件
try {
file.write(textString.getBytes());//根据函数参数的类型,写入内容
file.flush();//清楚缓存
Toast.makeText(getApplicationContext(), "保存成功", 2).show();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
//退出
Button quit=(Button)findViewById(R.id.quit);
quit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
//读取
Button read=(Button) findViewById(R.id.read);
read.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try {
//--------------------------------------------------------区别3---------------------------------------------------------
FileInputStream readFile=new FileInputStream(waitFile);//准备文件输入流对象
try {
byte [] buffer=new byte[readFile.available()];//实例化字节数组
readFile.read(buffer);//读取
readFile.close();//关闭
String data=new String(buffer);
EditText textEdit=(EditText)findViewById(R.id.text);
textEdit.setText(data);
System.out.println(data.toString());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
//获取文件列表
String [] fileList=fileList();
for(int i=0;i<fileList.length;i++){
System.out.println(fileList[i]);
}
System.out.println("绝对路径"+getFilesDir());
}
}