在 Kotlin 中移动 data/data 个文件
Move data/data files in Kotlin
我正在制作一个用于编辑系统文件(data/data 文件夹)的应用程序。我知道该应用程序需要 root 权限。如何将文件从一个 data/data 文件夹移动到另一个文件夹。我以为执行bash命令会更容易制作。
也许您还有一些关于使用 root 构建应用程序的有用信息或文档
我试过了
but.setOnClickListener {
root
var testName = "bruh.txt"
var text = "Help me"
File( filesDir , testName).writeText(text)
var dstString = "/data/data/com.test.testinproj/code_cache/"
var srcString = "/data/data/com.test.testinproj/files/bruh.txt"
Runtime.getRuntime().exec("mv \"$srcString\" \"$dstString\"")
我预计它会在 filesDir 中创建名为 bruh.txt 的文件,并将其移动到我需要的目录
您可以在 Kotlin 中复制文件而无需调用操作系统 exec。
File(srcString).copyTo(
target = File(dstString, "bruh.txt")
)
您也可以copy files recursively,如果您有多个文件要处理,这可能会更容易。
如果您需要确保目标文件和目录可写:
val destFile = File(dst, "bruh.txt").apply {
if (!parentFile.canWrite()) {
throw IOException("cannot write to $parent")
} else if (exists() && !canWrite()) {
throw IOException("cannot write to ${this.absolutePath}")
}
}
File(srcString).copyTo(
target = destFile
)
我正在制作一个用于编辑系统文件(data/data 文件夹)的应用程序。我知道该应用程序需要 root 权限。如何将文件从一个 data/data 文件夹移动到另一个文件夹。我以为执行bash命令会更容易制作。
也许您还有一些关于使用 root 构建应用程序的有用信息或文档
我试过了
but.setOnClickListener {
root
var testName = "bruh.txt"
var text = "Help me"
File( filesDir , testName).writeText(text)
var dstString = "/data/data/com.test.testinproj/code_cache/"
var srcString = "/data/data/com.test.testinproj/files/bruh.txt"
Runtime.getRuntime().exec("mv \"$srcString\" \"$dstString\"")
我预计它会在 filesDir 中创建名为 bruh.txt 的文件,并将其移动到我需要的目录
您可以在 Kotlin 中复制文件而无需调用操作系统 exec。
File(srcString).copyTo(
target = File(dstString, "bruh.txt")
)
您也可以copy files recursively,如果您有多个文件要处理,这可能会更容易。
如果您需要确保目标文件和目录可写:
val destFile = File(dst, "bruh.txt").apply {
if (!parentFile.canWrite()) {
throw IOException("cannot write to $parent")
} else if (exists() && !canWrite()) {
throw IOException("cannot write to ${this.absolutePath}")
}
}
File(srcString).copyTo(
target = destFile
)