Android Q 文件存储简单使用,以txt文件为例

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;
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值