当前位置:网站首页>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()
}
边栏推荐
猜你喜欢

初识Opencv4.X----方框滤波

*6-2 CCF 2015-03-3 Festival

@3-1 CCF 2020-09-1 scale detection point query

Redis string 结构命令

In depth interpretation of C language random number function and how to realize random number

对象数据如何转化成数组

Data query language (DQL)

OC--初识

用kotlin怎么写Android切换界面

~1 CCF 2022-06-2 treasure hunt! Big adventure!
随机推荐
OC -- category extension agreement and delegation
OC -- Foundation -- Collection
Surfaceview flash screen (black problem)
Use of map () function in JS
自定义 view 实现兑奖券背景[初级]
Definition of cell
cell的定义
[code source] I have a big head for a problem every day
一张图讲解 SQL Join 左连 又连
浏览器访问swagger失败,显示错误ERR_UNSAFE_PORT
Neural network method -- Boston house price (regression problem)
Browser access to swagger failed with error err_ UNSAFE_ PORT
[gplt] 2022 popular lover (Floyd)
学生管理系统(总结)
OC -- Foundation -- string + date and time
初识Opencv4.X----图像直方图绘制
@2-1 safety index predicted by CCF at the end of December 1, 2020
OC--Foundation--集合
Flex 布局语法与用例
【深度学习】卷积神经网络