How to read a file from the Internet using coroutines?

Asynchronous or non-blocking programming is the new reality. When creating server-side, desktop or mobile applications, it’s important to provide an experience that is not only fluid from the user’s perspective, but scalable when needed.

Kotlin solves this problem in a flexible way by providing coroutine support at the language level and delegating most of the functionality to libraries.

Let say we have a file at certain URL and to read that file we are taking the help of Coroutines.

All you need to do is launch it within a coroutine context, like this

private suspend fun urlRead() = withContext(Dispatchers.IO) {
val url = URL(MY_URL)
val stream = url.openStream()

where MY_URL is the URL where file is located.then you call it like this

lifecycleScope.launch {
val v = urlRead()
// TODO; use v

thats it.

you can also view this reference.




Data Analysis and ML| UI Designer | Python |Tableau |AR |Arduino |React Native

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Handle Synchronization in Selenium

.py Tech Series — Day 1 — Introduction to Programming and Terms

Advantages of Enterprise Integration

Implementing a tree structure in Django- part 1

Mouselook In Unity

Docker desktop, microk8s and the battle for the k8s laptop

SwiftUI Custom Modifiers

A Case of a Broken Netconsole

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akash Singh

Akash Singh

Data Analysis and ML| UI Designer | Python |Tableau |AR |Arduino |React Native

More from Medium

Android library distribution with multiple modules and flavors to Maven Central

Dagger with Android Achitecture Components using Kotlin

Kotlin extension function with example

Android Export and Import Data