Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想读取文件内容并为其内容的每一行发出一个流。所以,我必须实现一个具有以下签名的函数:
fun InputStream.linesToFlow(): Flow<String>
有没有办法实现这个功能?
我也找到了以下解决方案:
fun InputStream.linesToFlow() = bufferedReader().lineSequence().asFlow().flowOn(Dispatchers.IO)
你可以这样做
fun InputStream.linesToFlow(): Flow<String> = channelFlow<String> { reader().forEachLine { line -> offer(line) } }