读写文件添加权限
- 内部存储读写不需要添加任何权限
- 外部存储需要写需要添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
外部存储读不需要添加权限
- 外部存储代码实现
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File file = new File(Environment.getExternalStorageDirectory(), "info.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write((name + "##" + pass).getBytes()); fos.close(); }
Environment.getExternalStorageState()外部存储SD卡的状态
- UNMOUNT:SD卡已插,没有挂载
- CHECKING :SD卡正在被系统遍历
- MOUNTED:SD卡可以读写
- MOUNTED_READ_ONLY:SD卡可用但是只读
- 外部存储代码实现
文件访问权限
d rwx rwx rwx(r表示读,w表示写,x表示执行)
d:表示文件夹
第一个rwx表示决定owner对此文件有什么权限
第二个rwx表示和owner同一组的grouper用户对次文件有什么权限
第三个rwx表示决定other用户对此文件有什么权限读取其他应用文件
File file=new File("data/data/其他应用包名/files/info.txt")
效果图
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"/>
<EditText
android:id="@+id/et_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cb_checked"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住用户名和密码"/>
<Button
android:onClick="login"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆"/>
</RelativeLayout>
</LinearLayout>
主要代码
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends Activity {
private EditText et_name;
private EditText et_pass;
private CheckBox cb_checked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
et_name= (EditText) findViewById(R.id.et_name);
et_pass= (EditText) findViewById(R.id.et_pass);
cb_checked= (CheckBox) findViewById(R.id.cb_checked);
readInfo();
}
public void readInfo(){
try {
// 1.FileInputStream fis=openFileInput("info.txt");
// 读取data/data/com.example.test/files/info.txt下的文件
File file=new File(getFilesDir(),"info.txt");
if(file.exists()) {
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String s = reader.readLine();
String[] ss = s.split("##");
et_name.setText(ss[0]);
et_pass.setText(ss[1]);
Log.e("test", Environment.getExternalStorageDirectory().toString());
Toast.makeText(this, "name->" + ss[0] + ",password->" + ss[1], Toast.LENGTH_LONG).show();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void login(View v){
if(cb_checked.isChecked()){
String name=et_name.getText().toString();
String pass=et_pass.getText().toString();
try {
// 内部存储路径data/data/包名/下
// 外部存储路径sdcard/info.txt(或者Environment.getExternalStorageDirectory()+info.txt)
//
// 1. FileOutputStream fos=openFileOutput("info.txt", MODE_PRIVATE );
// openFileOutput()存储的文件目录在data/data/包名/files/info.txt
// 2.FileOutputStream fos=new FileOutputStream("data/data/com.example.test/info.txt");
// 3.File file=new File(getFilesDir(),"info.txt");
// FileOutputStream fos=new FileOutputStream(file);
// getFilesDir()===>data/data/com.example.test/files/info.txt
// getCacheDir()===>data/data/com.example.test/cache/info.txt
File file=new File(getFilesDir(),"info.txt");
FileOutputStream fos=new FileOutputStream(file);
fos.write((name+"##"+pass).getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}