Android创建本地文件夹、创建本地文件以及访问本地文件

在此之前,需要获得读写存储器的权限,在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创建本地文件夹

1.1.声明路径:
此处我使用的路径为:

 path = getExternalFilesDir("exter_test").getPath();

其中getExternalFilesDir(“exter_test”)是获取“exter_test”文件夹的路径,getPath()是取其相对路径;
1.2.设定文件夹名:
此处我设置为:

fileName = "test.txt";

1.3.编辑界面

界面中放置一个Button和一个EditText,并为Button绑定事件。

<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.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创建本地文件

2.1~2.3与1.1~1.3相同
2.4.MainActivity中内容

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访问本地文件

3.1~3.3与1.1~1.3相同
3.4.MainActivity中内容

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;
    }

源工程代码
———————————–2018.03.08———————————–

你可能感兴趣的:(Android)