This article shares the specific code of Android reading and writing file tool for your reference. The specific content is as follows
public class Utils { private static String path1 = ().getAbsolutePath(); private static String path2 = ().getAbsolutePath(); private static String pathExt = "/111/222/333/444/555/"; private static String fileName = ""; public static void write(String str) { String filePath = null; boolean hasSDCard =().equals(Environment.MEDIA_MOUNTED); if (hasSDCard) { filePath = path1 + pathExt + fileName; } else { filePath = path2 + pathExt + fileName; } try { File file = new File(filePath); if (!()) { //mkdirs() method generates multi-layer folder //mkdir() method generates layers of folders// File dir = new File(()); // (); ().mkdirs();//Create the folder outside the file ();// Generate file } FileOutputStream os = new FileOutputStream(file); (()); (); } catch (Exception e) { (); } } public static String read() { String content = ""; String filePath; boolean sdcard = ().equals(Environment.MEDIA_MOUNTED); if (sdcard) { filePath = path1 + pathExt + fileName; } else { filePath = path2 + pathExt + fileName; } try { File file = new File(filePath); if (()) { FileInputStream is = new FileInputStream(file); InputStreamReader inputReader = new InputStreamReader(is);//Set stream reading method BufferedReader buffReader = new BufferedReader(inputReader); String line; try { while (null != (line = ())) { content += line + "\n";//Read file content } ();//Close the input stream } catch (IOException e) { (); } finally { try { if (null != is) { (); } } catch (IOException e) { (); } } } } catch (FileNotFoundException e) { (); } return content; } }
1. Add read and write permissions to the manifest file
<uses-permission android:name=".WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name=".READ_EXTERNAL_STORAGE" />
2. Android 6.0 or above must dynamically apply for read and write permissions
ArrayList<String> permissionList = new ArrayList<>(); private String[] permissions = { ".READ_EXTERNAL_STORAGE", ".WRITE_EXTERNAL_STORAGE" }; //Check whether there is permission to write//Judge the mobile version. If it is lower than 6.0, you do not need to apply for permission and take photos directlyif (.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) { (permissions[0]); } if (checkSelfPermission(permissions[1]) != PackageManager.PERMISSION_GRANTED) { (permissions[1]); } if (!()) { String[] permissions1 = (new String[()]); requestPermissions(permissions1, 1); } else { ("balabala"); (); } } else { ("balabala"); (); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 1: if (PackageManager.PERMISSION_GRANTED == grantResults[0]){ ("balabala"); (); } else { (TAG, "fail: "); } break; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.