public class TxtUtil{ private static final String TXT = "/txt/"; private static volatile TxtUtil instance; private static final Uri externalUri = MediaStore.Downloads.EXTERNAL_CONTENT_URI; private static final String TXT_PATH = Environment.DIRECTORY_DOWNLOADS + TXT; public static TxtUtil getInstance(){ if(instance == null){ synchronized(TxtUtil.class){ if(instance == null){ instance = new TxtUtil(); } } } return instance; } /** * 写txt文件 */ public void writeTxt(String fileName, String content){ Log.e("VERSION", Build.VERSION.SDK_INT + ""); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){ deleteFile(fileName); createSAFTxt(fileName, content); }else{ } } private void createSAFTxt(String fileName, String content){ ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.Downloads.DISPLAY_NAME, fileName); contentValues.put(MediaStore.Downloads.RELATIVE_PATH, TXT_PATH); Uri insertResult = App.getInstance().getContentResolver().insert(externalUri, contentValues); if(insertResult != null){ Toast.makeText(App.getInstance(), "创建文件成功", Toast.LENGTH_SHORT).show(); try{ OutputStream outputStream = App.getInstance().getContentResolver().openOutputStream(insertResult); PrintWriter printWriter = new PrintWriter(outputStream); printWriter.print(content); printWriter.flush(); printWriter.close(); }catch(Exception e){ e.printStackTrace(); } } } /** * 删除txt 文件 */ public void deleteFile(String fileName){ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){ deleteSAFFile(fileName); }else{ } } private void deleteSAFFile(String fileName){ String selection = MediaStore.Downloads.DISPLAY_NAME + "=? and " + MediaStore.Downloads.RELATIVE_PATH + "=?"; String[] selectionArgs = new String[]{fileName, TXT_PATH}; //使用uri参数来确定查询哪张表,projection参数用于确定查询哪些列,selection和selectionArgs参数用于约束查询哪些行; // sortorder参数用于对结果进行排序,查询的结果存放在Cursor对象中返回。 int delete = App.getInstance().getContentResolver().delete(externalUri, selection, selectionArgs); Log.e("delete:___________", delete + ""); } /** * 删除txt 文件 */ public String readFile(String fileName){ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){ return readSAFFile(fileName); }else{ } return ""; } @SuppressLint("ShowToast") private String readSAFFile(String fileName){ String selection = MediaStore.Downloads.DISPLAY_NAME + "=? and " + MediaStore.Downloads.RELATIVE_PATH + "=?"; String[] selectionArgs = new String[]{fileName, TXT_PATH}; Cursor query = App.getInstance().getContentResolver().query(externalUri, null, selection, selectionArgs, null); String uriPath = ""; if(query != null && query.moveToFirst()){ uriPath = query.getString(query.getColumnIndexOrThrow(MediaStore.Downloads.DATA)); } if(query != null){ query.close(); } String content = ""; if(uriPath != null && uriPath.length() > 0){ InputStream instream; try{ instream = new FileInputStream(uriPath); InputStreamReader inputStreamReader = new InputStreamReader(instream); BufferedReader buffreader = new BufferedReader(inputStreamReader); String line; while((line = buffreader.readLine()) != null){ content += line; } buffreader.close(); }catch(Exception e){ e.printStackTrace(); } } return content; }
/** 重命名txt 文件 */ public boolean renameFile(String oldFileName, String newFileName){ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){ String filePath = getFilePath(oldFileName); File file = new File(filePath); File file2 = new File(filePath.replace(oldFileName, newFileName)); if(file.exists()){ return file.renameTo(file2); } }else{ } return false; }
private String getFilePath(String fileName){ String selection = MediaStore.Downloads.DISPLAY_NAME + "=? and " + MediaStore.Downloads.RELATIVE_PATH + "=?"; String[] selectionArgs = new String[]{fileName, TXT_PATH}; Cursor query = App.getInstance().getContentResolver().query(externalUri, null, selection, selectionArgs, null); String filePath = ""; if(query != null && query.moveToFirst()){ filePath = query.getString(query.getColumnIndexOrThrow(MediaStore.Downloads.DATA)); } if(query != null){ query.close(); } return filePath; } }