下载源码请移步至:http://download.csdn.net/detail/sserf/9792565
支持Android6.0+系统权限的获取和禁用,并且兼容以下系统
/******************************************创建文件夹/文件/并写入内容****************************/
/** * 向sdcard写文件 */ private void onWrite(String pathName, String fileName, String writeContent) { try { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + pathName); if (!path.exists()) { path.mkdirs();//创建目录 } File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + pathName + "/" + fileName); if (!f.exists()) { f.createNewFile();//创建文件 } OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(f)); //4.写文件,从EditView获得文本值 osw.write(writeContent); osw.close(); Toast.makeText(MainActivity.this,"创建成功",Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(MainActivity.this,"创建失败",Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } /*******************************************读取文件夹/文件中的内容*******************************/ private String read(String fileName) { String str = "内容为空"; if (Environment.getExternalStorageState().equals (Environment.MEDIA_MOUNTED)) { File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + fileName); Reader read; char[] b = null; int len = 0; if (f.exists()) { try { read = new InputStreamReader(new FileInputStream(f)); b = new char[1024]; len = read.read(b); str = new String(b, 0, len); read.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return str; } /*******************************************读取文件夹/文件中的内容****************************/ /*******************************************删除自定义下文件夹下中的文件*************************************/ /*删除自定义下文件夹下中的文件*/ public void deleteCacheFile(File file){ if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ if(file.exists()){ if(file.isFile()){//文件直接删除 file.delete(); } if(file.isDirectory()){//文件夹 File[] childfFiles=file.listFiles(); if(childfFiles==null||childfFiles.length==0){//文件夹不存在子文件(文件夹) file.delete(); }else {//文件夹中存在文件(子文件) for (File chilFile : childfFiles) { deleteCacheFile(chilFile); } file.delete(); } } Toast.makeText(MainActivity.this,"删除文件成功",Toast.LENGTH_SHORT).show(); } } }/*************************删除自定义下文件夹下中的文件**********结束*******************************/
![]()