Before this, you need to obtain read-write access to the memory. Add the following in AndroidManifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1.Android creates local folders
1.1. Declaration path:
The path I use here is:
path = getExternalFilesDir("exter_test").getPath();
Where getExternalFilesDir("extern? Test") is the path to get the folder "extern? Test", and getPath() is the relative path;
1.2. Set folder name:
Here I set it to:
fileName = "test.txt";
1.3. Editing interface
Place one in the interface Button And one EditText,Also for Button Binding event.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.a14553.localdocument.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:onClick="onClick"
android:text="Button" />
<EditText
android:id="@+id/editText"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textMultiLine" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
1.4. Content in mainactivity
public void onClick(View view){
newDirectory(path,"Test2");
//check();
//newFile(path,fileName);
//edt.setText(readFile(path,fileName));
// save(edt.getText().toString());
}
public void newDirectory(String _path,String dirName){
File file = new File(_path+"/"+dirName);
try {
if (!file.exists()) {
file.mkdir();
}
}catch (Exception e){
e.printStackTrace();
}
}
2.Android creates local files
2.1-2.3 is the same as 1.1-1.3
2.4. Main activity
public void onClick(View view){
//newDirectory(path,"Test2");
//check();
newFile(path,fileName);
//edt.setText(readFile(path,fileName));
// save(edt.getText().toString());
}
public void newFile(String _path,String _fileName){
File file=new File(_path+"/"+_fileName);
try {
if(!file.exists()) {
file.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
}
3.Android access to local files
3.1-3.3 same as 1.1-1.3
3.4. Main activity
public void onClick(View view){
newDirectory(path,"Test2");
//check();
//newFile(path,fileName);
//edt.setText(readFile(path,fileName));
// save(edt.getText().toString());
}
public String readFile(String _path,String _fileName){
String res = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(_path+"/"+_fileName)));
String line = "";
while ((line = reader.readLine())!=null){
res+=line;
}
}catch (IOException e){
e.printStackTrace();
}
return res;
}
Source engineering code
-----—–2018.03.08-----—–