一、創(chuàng)建文件夾和文件
// 獲取當(dāng)前包名的files路徑:/data/user/0/com.exa.myapplication/files
val PATH = this.filesDir.absolutePath
// 創(chuàng)建src和dst文件夾
// 【注】需要有PATH目錄的權(quán)限才能創(chuàng)建子目錄
// 若PATH文件夾權(quán)限為root權(quán)限,則使用adb shell chown命令修改權(quán)限
val src = File(PATH + "/" + "src")
// 判斷文件夾是否存在,不存在就進(jìn)行創(chuàng)建
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.")
}
}
// 創(chuàng)建info.txt文件,并寫(xiě)入數(shù)據(jù)———"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()
二、復(fù)制文件
將src目錄下的info.txt復(fù)制到dst目錄并重命名為info_dst.txt
1、 方法一:調(diào)用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()
// 設(shè)置目標(biāo)文件路徑
val dstPath = File("data/data/com.exa.myapplication/files/dst/info_dst.txt")
// 復(fù)制文件,第一個(gè)和第二個(gè)參數(shù)為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)
// 設(shè)置目標(biāo)文件路徑
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()
三、復(fù)制文件夾及其文件
/**
* 復(fù)制文件夾及其中的文件
* @param oldPath String 原文件夾路徑 如:data/data/com.exa.myapplication/files/src
* @param newPath String 復(fù)制后的路徑 如:data/data/com.exa.myapplication/files/dst
* @return `true` if and only if the directory and files were copied;
* `false` otherwise
*/
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是子文件夾,則繼續(xù)遞歸調(diào)用
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、刪除文件
只需要調(diào)用File的delete方法即可刪除指定文件
File("x.txt").delete()
2、刪除文件夾
如果文件夾不為空,調(diào)用delete方法是無(wú)法刪除文件夾的。需要先刪除文件夾中包含的所有文件,最后再刪掉文件夾。
fun deleteDirWihtFile(dir: File?) {
if (dir == null || !dir.exists() || !dir.isDirectory) return
for (file in dir.listFiles()) {
if (file.isFile) {
file.delete() // 刪除所有文件
} else if (file.isDirectory) {
deleteDirWihtFile(file) // 遞規(guī)的方式刪除文件夾
}
}
dir.delete() // 刪除目錄本身
}
五、設(shè)置文件的訪問(wèn)權(quán)限
1、方法一:使用File中的方法
val file = File("data/data/com.exa.myapplication/files/src/info.txt")
/**
* 參數(shù)一是executable:為true時(shí)設(shè)置權(quán)限;為false時(shí)沒(méi)有該權(quán)限
* 參數(shù)二是ownerOnly:為true時(shí)只對(duì)所有者生效;為false時(shí)對(duì)所有者,所在組和其它組都生效
*/
file.setReadable(true, false)
file.setWritable(true, false)
file.setExecutable(true, false)
2、方法二:執(zhí)行授權(quán)命令
val permissionsCmd = "chmod 777 data/data/com.exa.myapplication/files/src/info.txt"
Runtime.getRuntime().exec(permissionsCmd)
需要說(shuō)明的是:
讀寫(xiě)文件的前提是該文件具有讀寫(xiě)權(quán)限
復(fù)制文件和設(shè)置文件訪問(wèn)權(quán)限則需要app具有src和dst目錄的擁有者權(quán)限,一般的,app默認(rèn)是具有所在包名的權(quán)限為u0_a*:u0_a*(我這里是u0_a98:u0_a98),若目錄的擁有者權(quán)限為root:root,則app是無(wú)法在該目錄完成復(fù)制文件操作的,但可以通過(guò)Linux命令重新設(shè)置目錄權(quán)限,如下
adb shell chown system:system 目錄名 // app的AndroidManifest.xml需要添加android:sharedUserId="android.uid.system"
最后,給出linux中文件系統(tǒng)基本權(quán)限的說(shuō)明圖,方便查閱
【注意】
如果通過(guò)adb去push文件,該文件權(quán)限的擁有者和所屬組就不是u0_a98,而是其他,如u0_a0(如果執(zhí)行adb root成功了,會(huì)是root)。只要文件擁有者和所屬組不是u0_a98,app雖然可以讀,但沒(méi)有寫(xiě)權(quán)限,具體如下。
adb push new.txt data/data/com.test.pac/files
當(dāng)app去寫(xiě)new.txt文件時(shí),會(huì)提示文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-429908.html
W/System.err: java.io.FileNotFoundException: data/data/com.test.pac/files/new.txt: open failed: EACCES (Permission denied)
【解決辦法】
方法一:app自己創(chuàng)建文件,完成寫(xiě)操作
方法二:adb shell chown u0_a98:u0_a98 data/data/com.test.pac/files/new.txt文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-429908.html
到了這里,關(guān)于Android文件基本操作(創(chuàng)建文件(夾)、復(fù)制文件(夾)、設(shè)置文件訪問(wèn)權(quán)限)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!