Today we will learn how to download a file from URL in java. We can use java.net.URL openStream() method to download file from URL in java program. We can use Java NIO Channels or Java IO InputStream to read data from the URL open stream and then save it to file.
Java Download File from URL
downloadUsingStream: In this method of java download file from URL, we are using URL openStream method to create the input stream. Then we are using a file output stream to read data from the input stream and write to the file. downloadUsingNIO: In this download file from URL method, we are creating byte channel from URL stream data. Then use the file output stream to write it to file. You can use any of these methods to download the file from URL in java program. If you are looking for performance, then do some analysis by using both methods and see what suits your need.
You can checkout more Java IO examples from our GitHub Repository.
This post will discuss how to download a file from a remote URL in Kotlin.
1. Using FileChannel.transferFrom() function
The FileChannel#transferFrom() function is used to transfer bytes into this channel’s file from a readable byte channel. It accepts three parameters – the source channel, the position within the file to begin the transfer, and the maximum number of bytes to be transferred.
Its usage is demonstrated below using the use() function, which takes care of closing the opened streams and channels.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | importjava.io.FileOutputStream import java.net.URL import java.nio.channels.Channels fun downloadFile(url:URL,outputFileName:String){ url.openStream().use{ Channels.newChannel(it).use{rbc-> FileOutputStream(outputFileName).use {fos-> fos.channel.transferFrom(rbc,0, Long.MAX_VALUE) } } } } fun main(){ // call `downloadFile()` function } |
Download Code
2. Using Files.copy() function
We can also use the Files.copy() function to copy all bytes from an input stream to a file. It takes the input stream to read from and the path to the file and optional params indicating how copying should be done.
importjava.net.URL import java.nio.file.Files import java.nio.file.Paths fun downloadFile(url:URL,fileName: String){ url.openStream().use{Files.copy(it,Paths.get(fileName))} } fun main(){ // call `downloadFile()` function } |
Download Code
3. Using BufferedInputStream
Finally, we can read the file from the input stream byte-by-byte and write the bytes to a file output stream. This would translate to a simple code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | importjava.io.BufferedInputStream import java.io.FileOutputStream importjava.net.URL fun downloadFile(url:URL,fileName: String){ url.openStream().use{inp-> BufferedInputStream(inp).use{bis -> FileOutputStream(fileName).use {fos-> val data =ByteArray(1024) varcount:Int while (bis.read(data,0, 1024).also{count=it}!=-1){ fos.write(data,0,count) } } } } } fun main(){ // call `downloadFile()` function } |
Download Code
That’s all about downloading a file from a remote URL in Kotlin.
Thanks for reading.
Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and help us grow. Happy coding :)