栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android文件基本操作(创建文件(夹)、复制文件(夹)、设置文件访问权限)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android文件基本操作(创建文件(夹)、复制文件(夹)、设置文件访问权限)

Android文件基本操作
  • 一、创建文件夹和文件
  • 二、复制文件
  • 三、复制文件夹及其文件
  • 四、设置文件的访问权限

一、创建文件夹和文件
        // 获取当前包名的files路径:/data/user/0/com.exa.myapplication/files
        val PATH = this.filesDir.absolutePath
        // 创建src和dst文件夹
        // 【注】需要有PATH目录的权限才能创建子目录
        // 若PATH文件夹权限为root权限,则使用adb shell chown system:system PATH修改为system权限
        val src = File(PATH + "/" + "src")
        if (!src.exists()) {
            if (!src.mkdirs()){
                Log.e(TAG, "create directory failed.")
            }
        }
        val dst = File(PATH + "/" + "dst")
        if (!dst.exists()) {
            if (!dst.mkdirs()){
                Log.e(TAG, "create directory failed.")
            }
        }

        // 创建info.txt文件,并写入数据———"hello info"
        val srcPath = File("data/data/com.exa.myapplication/files/src/info.txt")
        val fos = FileOutputStream(srcPath)
        fos.write("hello info".toByteArray())
        fos.close()
二、复制文件

将src目录下的info.txt复制到dst目录并重命名为info_dst.txt
1、 方法一:调用java.nio.file.Files.copy()

		val srcPath = File("data/data/com.exa.myapplication/files/src/info.txt")		
        // 判断源文件是否存在、可读
        if (!srcPath.exists()){
            Log.i(TAG, "file is not exist.")
            return
        } else if (!srcPath.isFile){
            Log.i(TAG, "Not a file.")
            return
        } else if (!srcPath.canRead()){
            Log.i(TAG, "file is not readable.")
            return
        }        
        val fos = FileOutputStream(srcPath)
        fos.write("hello info".toByteArray())
        fos.close()
        // 设置目标文件路径
		val dstPath = File("data/data/com.exa.myapplication/files/dst/info_dst.txt")
        // 复制文件,第一个和第二个参数为PATH类型
        Files.copy(srcPath.toPath(), dstPath.toPath(), StandardCopyOption.REPLACE_EXISTING)

2、方法二:使用输入输出流

        val srcPath = File("data/data/com.exa.myapplication/files/src/info.txt")
        // 判断源文件是否存在、可读
        if (!srcPath.exists()){
            Log.i(TAG, "file is not exist.")
            return
        } else if (!srcPath.isFile){
            Log.i(TAG, "Not a file.")
            return
        } else if (!srcPath.canRead()){
            Log.i(TAG, "file is not readable.")
            return
        }
        val input = FileInputStream(srcPath)
		// 设置目标文件路径
        val output = FileOutputStream("data/data/com.exa.myapplication/files/dst/info_dst.txt")
        var length = -1
        val buf = ByteArray(1024)
        while(input.read(buf).also { length = it } != -1){
            output.write(buf, 0, length)
        }
        output.flush()
        input.close()
        output.close()
三、复制文件夹及其文件
	
    fun copyFolder(oldPath: String, newPath: String): Boolean {
        return try {
            val newFile = File(newPath)
            if (!newFile.exists()) {
                if (!newFile.mkdirs()) {
                    Log.e(TAG, "create directory failed.")
                    return false
                }
            }
            val oldFile = File(oldPath)
            // 获取oldPath路径下的全部文件
            val files = oldFile.list()
            var temp: File
            for (file in files) {
                temp = if (oldPath.endsWith(File.separator)) {
                    File(oldPath + file)
                } else {
                    File(oldPath + File.separator + file)
                }
                if (temp.isDirectory) {
                    // 如果temp是子文件夹,则继续递归调用
                    copyFolder("$oldPath/$file", "$newPath/$file")
                }else if (temp.isFile && temp.exists() && temp.canRead()){
                    val fileInputStream = FileInputStream(temp)
                    val fileOutputStream = FileOutputStream(newPath + "/" + temp.name)
                    val buffer = ByteArray(1024)
                    var byteRead: Int
                    while (fileInputStream.read(buffer).also { byteRead = it } != -1) {
                        fileOutputStream.write(buffer, 0, byteRead)
                    }
                    fileInputStream.close()
                    fileOutputStream.flush()
                    fileOutputStream.close()
                }
            }
            true
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }
四、设置文件的访问权限

1、方法一:使用File中的方法

        val file = File("data/data/com.exa.myapplication/files/src/info.txt")
        
        file.setReadable(true, false)
        file.setWritable(true, false)
        file.setExecutable(true, false)

2、方法二:执行授权命令

        val permissionsCmd = "chmod 777 data/data/com.exa.myapplication/files/src/info.txt"
        Runtime.getRuntime().exec(permissionsCmd)

需要说明的是:
读写文件的前提是该文件具有读写权限
复制文件和设置文件访问权限则需要app具有src和dst目录的拥有者权限,一般的,app默认是具有所在包名的权限为u0_a*:u0_a*(我这里是u0_a98:u0_a98),若目录的拥有者权限为root:root,则app是无法在该目录完成复制文件操作的,但可以通过Linux命令重新设置目录权限,如下

adb shell chown system:system 目录名	// app的AndroidManifest.xml需要添加android:sharedUserId="android.uid.system"


最后,给出linux中文件系统基本权限的说明图,方便查阅

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939807.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号