在此之前,需要获得读写存储器的权限,在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绑定事件。<?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.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———————————–

更多相关文章

  1. Android开发者指南(3) —— Other Tools
  2. Android(安卓)Studio 在run时报的异常 Failed to run command fi
  3. 用gdb调试 android webkit
  4. fragment UI
  5. Createing Dialogs/创建对话框
  6. Android(安卓)使用SWIG生成Jni代码
  7. android当前apn的状态以及获取方法
  8. 关于Studio和EclipseSHA1值的查看
  9. Android培训---创建Android工程

随机推荐

  1. 【专题报道】Google I/O开发者大会
  2. Android(安卓)自定义控件入门篇之自定义
  3. Android(安卓)进程保活手段分析
  4. Android:多语言对应
  5. Android创建和使用数据库
  6. Android(安卓)中LayoutInflater(布局加载
  7. Android(安卓)数字签名学习笔记
  8. Android(安卓)开发之旅:又见Hello World!
  9. Android开发者应该深入学习的10个开源应
  10. Android(安卓)实现颜色渐变的一个小 tip