本文實例為大家分享了Android讀寫文件工具類的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比柳江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式柳江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋柳江地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。
public class Utils { private static String path2 = Environment.getExternalStorageDirectory().getAbsolutePath(); private static String path3 = Environment.getDownloadCacheDirectory().getAbsolutePath(); private static String pathExt = "/111/222/333/444/555/"; private static String fileName = "6.txt"; public static void write(String str) { String filePath = null; boolean hasSDCard =Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); if (hasSDCard) { filePath = path2 + pathExt + fileName; } else { filePath = path3 + pathExt + fileName; } try { File file = new File(filePath); if (!file.exists()) { //mkdirs()方法生成多層文件夾 //mkdir()方法生成一層層文件夾 // File dir = new File(file.getParent()); // dir.mkdirs(); file.getParentFile().mkdirs();//生成文件外層的文件夾 file.createNewFile();//生成文件 } FileOutputStream os = new FileOutputStream(file); os.write(str.getBytes()); os.close(); } catch (Exception e) { e.printStackTrace(); } } public static String read() { String content = ""; String filePath; boolean sdcard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); if (sdcard) { filePath = path2 + pathExt + fileName; } else { filePath = path3 + pathExt + fileName; } try { File file = new File(filePath); if (file.exists()) { FileInputStream is = new FileInputStream(file); InputStreamReader inputReader = new InputStreamReader(is);//設(shè)置流讀取方式 BufferedReader buffReader = new BufferedReader(inputReader); String line; try { while (null != (line = buffReader.readLine())) { content += line + "\n";//讀取的文件容 } is.close();//關(guān)閉輸入流 } catch (IOException e) { e.printStackTrace(); } finally { try { if (null != is) { is.close(); } } catch (IOException e) { e.printStackTrace(); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } return content; } }
1. 清單文件中添加讀寫權(quán)限
2.Android6.0以上版本要動態(tài)申請讀寫權(quán)限
ArrayListpermissionList = new ArrayList<>(); private String[] permissions = { "android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE" }; //檢測是否有寫的權(quán)限 //判斷手機版本,如果低于6.0 則不用申請權(quán)限,直接拍照 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) { permissionList.add(permissions[0]); } if (checkSelfPermission(permissions[1]) != PackageManager.PERMISSION_GRANTED) { permissionList.add(permissions[1]); } if (!permissionList.isEmpty()) { String[] permissions1 = permissionList.toArray(new String[permissionList.size()]); requestPermissions(permissions1, 1); } else { Utils.write("balabala"); Utils.read(); } } else { Utils.write("balabala"); Utils.read(); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 1: if (PackageManager.PERMISSION_GRANTED == grantResults[0]){ Utils.write("balabala"); Utils.read(); } else { Log.d(TAG, "fail: "); } break; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。