How to download a file from a URL in Java

In this tutorial, we are going to see several methods that we can use to download a file.

We are going to see examples varying from basic use of Java IO to the NIO package to some common libraries such as Apache Commons IO.

How to download a file from a URL Using Java IO

The Java IO package is the basic package for downloading a file from a URL. Here, we’ll use BufferedInputStream and the URL classes to open and read a file from a given address to a file on our local system. The reason we are using the BufferedInputStream class instead of InputStream is its storage capacity which gives our code a performance boost.
The URL class in Java is a built-in library that provides several methods for accessing and manipulating data on the Internet. In this case, we’ll use the openStream() function of the URL class. The signature of the openStream() method is as follows:

public final InputStream openStream() throws IOException

The openStream() function works on an object of the URL class. The URL class opens a connection to the given URL, and the openStream() method returns an input stream used to read data from the connection.

The second class we’re going to use is BufferedInputStreamReader and FileOutputStream. These classes are used to read from and write to a file, respectively.

try (BufferedInputStream bis = new BufferedInputStream(new URL("").openStream());  
  FileOutputStream fos = new FileOutputStream("/Desktop/myfile.txt")) {
    byte data[] = new byte[1024];
    int byteContent;
    while ((byteContent =, 0, 1024)) != -1) {
        fos.write(data, 0, byteContent);
} catch (IOException e) {


How to download a file from a URL Using Java NIO

Java NIO is an alternative package for managing network operations and I/O in Java. The advantage of the Java NIO package is that it does not crash and has piping and storage functionality. When we use the Java IO library, we are working with streams that read data byte by byte. On the other hand, the Java NIO package uses channels and buffers. The storage and piping functions allow the system to copy the contents of a URL directly to the desired file without having to store the bytes in application memory, which would be an intermediate step. The ability to work with channels improves performance.

In order to download the contents of a URL, we will use the ReadableByteChannel and FileChannel classes.

ReadableByteChannel rbc = Channels.newChannel(new URL("").openStream());

The class ReadableByteChannel initiate a stream to read content from the URL. The downloaded content will be transferred to a file on the local system through the corresponding channel.

FileOutputStream fos = new FileOutputStream("/Desktop/myfile.txt");  
FileChannel channel = fos.getChannel();

After setting the file channel, we’ll use the transferFrom() method to copy the content from the “rbc” object to the file destination using the “channel” object.

channel.transferFrom(rbc, 0, Long.MAX_VALUE);


How to download a file from a URL Using Apache Commons IO

The Apache Commons IO library provides a list of classes for handling IO operations. Now you might be wondering why we are using this when Java has its own libraries to handle IO operations. However, Apache Commons IO solves the problem of rewriting code and avoids writing standard code.

To download a file from a given URL with Apache Commons IO, we need the FileUtils class. Only one line of code is needed to download a file, which looks like this:

  new URL(""), 
  new File("/Desktop/myfile.txt"), 



We have seen in this Tutorial several ways to download a file from a URL in Java. The most common implementation is where we remember bytes when performing read/write operations. This implementation is safe to use even for large files because we are not loading the entire file into memory.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *