android的文件操作要有权限:

  判断SD卡是否插入

  Environment.getExternalStorageState().equals(

  android.os.Environment.MEDIA_MOUNTED);

  获得sd卡根目录

  File skRoot = Environment.getExternalStorageDirectory();

  获得私有根目录

  File fileRoot = Context.getFilesDir()+"/";

  确定或获得文件夹和文件路径

  String path = File.getPath();//相对

  String path = File.getAbsoultePath();//绝对

  获得文件或文件夹的父目录

  String parentPath = File.getParent()

  获得文件或文件夹的名称:

  String Name = File.getName();

  建立文件或文件夹

  File.createNewFile();//建立文件

  判断是文件或文件夹

  File.isDirectory()

  列出文件夹下的所有文件和文件夹名

  File[] files = File.listFiles();

  修改文件夹和文件名

  File.renameTo(dest);

  删除文件夹或文件

  File.delete();

  文件读写操作模式

  Context.MODE_PRIVATE:新内容覆盖原内容

  Context.MODE_APPEND:新内容追加到原内容后

  Context.MODE_WORLD_READABLE:允许其他应用程序读取

  Context.MODE_WORLD_WRITEABLE:允许其他应用程序写入,会覆盖原数据。

  /*** 保存文件

  * @param fileName

  * @param fileContent

  * @throws Exception*/

  public void save(String fileName, String fileContent) throws Exception {

  // Activity的父类的父类就是context,context与其他框架中的context相同为我们以供了一些核心操作工具。

  FileOutputStream fileOutputStream = this.context.openFileOutput(

  fileName, Context.MODE_PRIVATE);

  fileOutputStream.write(fileContent.getBytes());

  }

  /**

  * 读取文件

  *

  * @param fileName

  * @return

  * @throws Exception

*/

  public String read(String fileName) throws Exception {

  FileInputStream fileInputStream = this.context.openFileInput(fileName);

  ByteArrayOutputStream byteArray = new ByteArrayOutputStream();

  byte[] buffer = new byte[1024];

  int len = 0;

  while ((len = fileInputStream.read(buffer)) > 0) {

  byteArray.write(buffer, 0, len);

  };

  return byteArray.toString();

  }

  }

更多相关文章

  1. android 获取联系人和短消息信息
  2. Android判断文件类型(视频、音频、图片等)
  3. [Android(安卓)Studio][NDK]Execution failed for task ':app:co
  4. Android(安卓)播放MP4文件
  5. Android(安卓)基础布局控件自定义view使用练习
  6. Android(安卓)studio 导入github工程
  7. android html 读写文件
  8. 向SD卡写文件
  9. NPM 和webpack 的基础使用

随机推荐

  1. Android软键盘用法实例
  2. Ubuntu amd64 running Android(安卓)SDKs
  3. android 代码 退出程序
  4. Android中Parcelable和Serializable接口
  5. API-TypedArray
  6. Android全局处理异常
  7. android 幕滑动效果
  8. Android(安卓)– 加载图片本缓存到内存与
  9. android audio
  10. Android——截图案例