1.获取assets文本内容
fun readAssetsTxt(fileName: String): String?
{
val input = assets.open("$fileName")
val inputstreamreader = BufferedReader(InputStreamReader(input))
try
{
Log.e("获取的assets文本内容----", inputstreamreader.readLines().toString()!!)
return inputstreamreader.readLines().toString()
}
catch (e: IOException)
{
e.printStackTrace()
}
finally
{
inputstreamreader.close()
}
return "读取失败,请检查文件名称及文件是否存在!"
}
2.获取网址文本内容
2.1方法
fun readTxt(url: String?): String?
{
val url = URL(url)
val inputstreamreader = BufferedReader(InputStreamReader(url.openStream()))
try
{
Log.e("获取网址文本内容---------", inputstreamreader.readLines().toString())
return inputstreamreader.readLines().toString()
}
catch (e: java.lang.Exception)
{
e.printStackTrace()
}
finally
{
inputstreamreader.close()
}
return "读取失败,请检查文件名称及文件是否存在!"
}
2.2 使用
这里要开一个线程,不然会报网络请求不能占用主线程的错误
Thread {readTxt("https://xxxxxxxxxx/text.txt")}.start()