Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:38:16+00:00 2026-06-09T20:38:16+00:00

I was browsing the Internet for random Java code, and I found this source

  • 0

I was browsing the Internet for random Java code, and I found this source code for a download manager. It uses RandomAccessFile to download the files. The one thing I could not figure out though, was where it would download to. Here is the method that downloads the file:

public void startDownload() {
    System.out.println("Starting...");
    RandomAccessFile file = null;
    InputStream stream = null;

    try {
        URL downloadLink = new URL("http://www.website.com/file.txt");

        // Open the connection to the URL
        HttpURLConnection connection = (HttpURLConnection) downloadLink.openConnection();

        // Specify what portion of file to download
        connection.setRequestProperty("Range", "bytes=" + downloaded + "-");

        // Connect to the server
        connection.connect();

        // Make sure the code is in the 200 range
        if (connection.getResponseCode() / 100 != 2) {
            error();
        }

        // Check for valid content length
        int contentLength = connection.getContentLength();
        if (contentLength < 1) {
            error();
        }

        // Set the size for the download if it hasn't been already set
        if (size == -1) {
            size = contentLength;
            stateChanged();
        }

        // Open file and seek to the end of it
        file = new RandomAccessFile(getFileName(downloadLink), "rw");  
              // getFileName returns the name of the file mentioned in the URL

        file.seek(downloaded);

        stream = connection.getInputStream();

        while (status == DOWNLOADING) {
            System.out.println("Progress: " + getProgress() + "%");

            // Size the buffer according to how much of the file is left to download
            byte buffer[];
            if (size - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[size - downloaded];
            }

            // Read from the server into the buffer
            int read = stream.read(buffer);
            if (read == -1) {
                break;
            }

            // Write buffer to file
            file.write(buffer, 0, read);
            downloaded += read;
            stateChanged();
        }

        if (status == DOWNLOADING) {
            status = COMPLETE;
            stateChanged();
        }


    } catch (Exception e) {
        error();
    } finally {
        // Close the stream and RAF
    }
    System.out.println("Done!");
}

I am sorry if this is obvious. I am new to the RandomAccessFile class, as I just learned of it today.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:38:17+00:00Added an answer on June 9, 2026 at 8:38 pm

    It will download it in the current working directory (i.e. where you run your java command) and the name of the file will be given by getFileName(downloadLink).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently found some interesting macro while browsing random sources around the internet: #define
Browsing the Internet, I found the new Ample SDK JavaScript framework. From their about
I was browsing for a while the internet and this site and instead of
So I was browsing the internet one day, and copied a chunk of text
I have been browsing around on the internet and researching effective sitemap web pages.
browsing some html code I find a text input coded as: <input type=text name=Email<%=orderRow.ID
While browsing I came across this blog post about using the Wikipedia API from
While browsing MSDN documentation, you may come across this gem: TextBox.Watermark. Awesome! I've been
I am receiving the following error when browsing my asp.net page in internet explorer.
I was browsing the internet lately, when I stumbled upon Dll Injection. I think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.