android文件操作

本文介绍了在Android 10之前如何获取本地文件目录,包括使用Environment.getExternalStorageDirectory().getAbsolutePath()的方法,以及在AndroidManifest.xml中添加读写权限的必要性。同时,文章还提及了在Android 10中,由于权限问题,无法直接通过上述方法创建文件,需要改用getExternalFilesDir(null)来获取安全的文件存储路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android10 之前获取本地文件目录:

在android 10之前使用 Environment.getExternalStorageDirectory().getAbsolutePath()可获取sd卡中的文件存储目录,如果需要进行文件读取,需要在AndroidManifest.xml文件里面添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

还需要获取动态权限:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                    checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            } 
        } 

然后是创建文件夹及写入文件数据:

 String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        File dirFile = new File(path);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            dirFile.mkdirs();
        }

        AssetManager assets = getAssets();
        try {
            File dataFile = new File(path, "mine.dat");
            if (!dataFile.exists() || dataFile.isDirectory()) {
                boolean file = dataFile.createNewFile();
                System.out.println("创建文件:" + file);
            }
            InputStream is = assets.open("bd_etts_text.dat");
            FileOutputStream fos = new FileOutputStream(dataFile);
            byte[] buffer = new byte[1024 * 1024];
            int count = 0;
            while ((count = is.read(buffer)) != -1) {
                fos.write(buffer, 0, count);
            }
            fos.flush();
            fos.close();
            is.close();
            System.out.println("文件下载到本地成功");
        } catch (IOException e) {
            System.out.println("文件下载出错!");
            e.printStackTrace();
        }

2、android 10创建文件

在AndroidManifest.xml中添加权限,并添加动态权限。Environment.getExternalStorageDirectory().getAbsolutePath()获取的文件路径是不能创建文件及文件夹,系统会报java.io.IOException: Permission denied异常,so创建文件需使用其他路径:

String path = getExternalFilesDir(null).getAbsolutePath() + "/cdd/app";
File dirFile = new File(path);
if (!dirFile.exists() || !dirFile.isDirectory()) {
   dirFile.mkdirs();
}
AssetManager assets = getAssets();
try {
   File dataFile = new File(path, "mine.dat");
   if (!dataFile.exists() || dataFile.isDirectory()) {
       boolean file = dataFile.createNewFile();
    }
    InputStream is = assets.open("bd_etts_text.dat");
    FileOutputStream fos = new FileOutputStream(dataFile);
    byte[] buffer = new byte[1024 * 1024];
    int count = 0;
    while ((count = is.read(buffer)) != -1) {
        fos.write(buffer, 0, count);
    }
    fos.flush();
    fos.close();
    is.close();
    System.out.println("文件下载到本地成功");
} catch (IOException e) {
    System.out.println("文件下载出错!");
    e.printStackTrace();
}

需要用getExternalFilesDir(null)方法来获取文件存储路径。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值