当前位置:网站首页>Use kotlin use to simplify file reading and writing
Use kotlin use to simplify file reading and writing
2022-07-25 09:43:00 【Flying time machine】
Use Kotlin USE Simplify file reading and writing
Use Kotlin USE to shorten file reading
Conventional writing
Regular file reading and writing are as follows ;
private val pdfReader by lazy {
val file = File(cacheDir, fileName)
val outputStream = FileOutputStream(file)
try {
val inputStream = assets.open(fileName)
var bytesCopied: Long = 0
val buffer = ByteArray(8 * 1024)
var bytes = inputStream.read(buffer)
while (bytes >= 0) {
outputStream.write(buffer, 0, bytes)
bytesCopied += bytes
bytes = inputStream.read(buffer)
}
} finally {
outputStream.close()
}
PdfReader(file)
}
Use Use
Kotlin1.2 Start , Provide a use Extension method , Can be used in Closeable Class
The above code can be reduced to :
private val pdfReader by lazy {
val file = File(cacheDir, fileName)
val outputStream = FileOutputStream(file)
outputStream.use { outputStream ->
val inputStream = assets.open(fileName)
var bytesCopied: Long = 0
val buffer = ByteArray(8 * 1024)
var bytes = inputStream.read(buffer)
while (bytes >= 0) {
outputStream.write(buffer, 0, bytes)
bytesCopied += bytes
bytes = inputStream.read(buffer)
}
}
PdfReader(file)
}
Benefit time Copy-to
above Stream copy will also have an extension . InputStream.copyTo
private val pdfReader by lazy {
val file = File(cacheDir, fileName)
val outputStream = FileOutputStream(file)
outputStream.use {
fileOut ->
assets.open(fileName).copyTo(fileOut)
}
PdfReader(file)
}
Benefit time 2 outputStream() Expand
File class There is also an extension function outputStream()
private val pdfReader by lazy {
val file = File(cacheDir, fileName)
file.outputStream().use {
fileOut ->
assets.open(fileName).copyTo(fileOut)
}
PdfReader(file)
}
WOW , Transformation completed ,12 Line code , Reduced to 3 That's ok .
Extracurricular reading :
InputStream Read out character string (Text,String)
val myString = inputStream.bufferedReader().use {
bufferReader ->
bufferReader?.readText()
}
边栏推荐
- Flex 布局语法与用例
- The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do
- Database operation language (DML)
- Operation 7.19 sequence table
- 【cf】Round 128 C. Binary String
- OC--Foundation--集合
- How to write Android switching interface with kotlin
- [code source] a prime number of fun every day (BFS)
- Redis set structure command
- 解决esp8266无法连接手机和电脑热点的问题
猜你喜欢

Flex layout syntax and use cases

初识Opencv4.X----为图像添加椒盐噪声

初识Opencv4.X----图像直方图均衡

¥ 1-3 SWUST OJ 942: reverse sequence table

Kotlin collaboration: foundation and use of collaboration

【Android studio】批量数据导入到android 本地数据库

如何将Jar包部署到服务器,注:启动命令有无nohup有很大关系

基于机智云平台的温湿度和光照强度获取

OC--类别 扩展 协议与委托

Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
随机推荐
chmod和chown对挂载的分区的文件失效
[code source] National Railway
Kotlin collaboration: foundation and use of collaboration
Jar包在阿里云服务器起起来了,安全组也开通了,但postman仍跑不通怎么办
OC -- Foundation -- string + date and time
Redis database foundation
What is cerebral fissure?
Basic network knowledge
@3-1 CCF 2020-09-1 scale detection point query
App的生命周期和AppleDelegate,SceneDelegate
~1 CCF 2022-06-2 treasure hunt! Big adventure!
@4-1 CCF 2020-06-1 linear classifier
Android 如何使用adb命令查看应用本地数据库
*6-3 save small experts
学习 Redis linux 安装Redis
expect+sh实现自动交互
什么是脑裂问题?
[code source] a prime number of fun every day (BFS)
*6-1 CCF 2015-03-2 numerical sorting
Surfaceview flash screen (black problem)