作者:snow_lyGirl
链接:https://blog.csdn.net/qq_31028313/article/details/125299445
来源:CSDN小记一下
1、保存内容到文件及文件读取
/**
* 保存信息到File
* @param fileName 文件名
* @param content 保存内容
* @param mode 模式
*/
fun Context.saveIntoFile(
fileName: String,
content: String,
mode: Int = Context.MODE_PRIVATE
): Boolean {
return try {
openFileOutput(fileName, mode).use {
it.write(content.toByteArray())
true
}
} catch (e: Exception) {
false
}
}
/**
* 读取文件
* @param fileName 文件名
*/
fun Context.readFromFile(fileName: String): String {
val sb = StringBuilder()
openFileInput(fileName).use {
val temp = ByteArray(1024)
var len = 0
while (true) {
len = it.read(temp)
if (len <= 0) break
sb.append(String(temp, 0, len))
}
}
return sb.toString()
}
在Activity中使用
//保存
saveIntoFile(FILE_NAME, content)
//读取
val content = readFromFile(FILE_NAME)
2、保存内容到SD卡中的文件及SD卡文件读取
/**
* 写文件到SD卡
* eg:/storage/emulated/0/Android/data/com.guc.androiddemo/files/Documents/fileName
* @param fileName 文件名
* @param content 内容
* @param type 文件类型
* {@link android.os.Environment#DIRECTORY_MUSIC},
* {@link android.os.Environment#DIRECTORY_PODCASTS},
* {@link android.os.Environment#DIRECTORY_RINGTONES},
* {@link android.os.Environment#DIRECTORY_ALARMS},
* {@link android.os.Environment#DIRECTORY_NOTIFICATIONS},
* {@link android.os.Environment#DIRECTORY_PICTURES}, or
* {@link android.os.Environment#DIRECTORY_MOVIES}.
*/
fun Context.saveFileToSD(
fileName: String,
content: String,
type: String = Environment.DIRECTORY_DOCUMENTS
) {
if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
val fn = getExternalFilesDir(type)?.canonicalPath + "/" + fileName
FileOutputStream(fn).use {
it.write(content.toByteArray())
}
} else {
throw Exception("没有存储卡")
}
}
/**
* 读取SD中文件
* eg:/storage/emulated/0/Android/data/com.guc.androiddemo/files/Documents/fileName
* @param fileName 文件名
* @param type 文件类型-Documents
*/
fun Context.readFromSD(
fileName: String,
type: String = Environment.DIRECTORY_DOCUMENTS
): String {
return if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
val sb = StringBuilder()
val fn = getExternalFilesDir(type)?.canonicalPath + "/" + fileName
FileInputStream(fn).use {
val temp = ByteArray(1024)
var len = 0
while (true) {
len = it.read(temp)
if (len <= 0) break
sb.append(String(temp, 0, len))
}
}
sb.toString()
} else {
throw Exception("没有存储卡")
}
}
Activity中直接使用
//保存
saveFileToSD(FILE_NAME, content)
//读取
val content = readFromSD(FILE_NAME)
3、读取Raw文件
/**
* 读取Raw文件中的内容
* @param rawId Raw文件ID:R.id.info
*/
fun Context.readRaw(rawId: Int): String {
return BufferedReader(InputStreamReader(resources.openRawResource(rawId))).use {
val sb = StringBuilder()
it.forEachLine { s ->
sb.append(s)
}
sb.toString()
}
}
4、读取Assets文件
/**
* 读取Raw文件中的内容
* @param rawId Raw文件ID:R.id.info
*/
fun Context.readRaw(rawId: Int): String {
return BufferedReader(InputStreamReader(resources.openRawResource(rawId))).use {
val sb = StringBuilder()
it.forEachLine { s ->
sb.append(s)
}
sb.toString()
}
}
版权属于:Monster_4y
本文链接:https://blog.zmonster.top/91.html
转载时须注明出处及本声明