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

  • SEARCH
  • Home
  • 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 8862311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:41:53+00:00 2026-06-14T15:41:53+00:00

The following code saves the page’s content to a file: import java.net.*; import java.io.*;

  • 0

The following code saves the page’s content to a file:

import java.net.*;
import java.io.*;



public class url
{
      public static void main(String[] args)
      {
           try
                  {
                    URL PageUrl;
                    URLConnection GetConn = null;
                    GetConn = null;

                     PageUrl = new URL("https://www.google.ru/");
                     GetConn = PageUrl.openConnection();

                    GetConn.connect();

                    InputStreamReader ReadIn = new InputStreamReader(GetConn.getInputStream());
                    BufferedReader BufData = new BufferedReader(ReadIn);
                    String htmlFileName = ("C:\\hello.html");
                    FileWriter FWriter = new FileWriter(htmlFileName);
                    BufferedWriter BWriter = new BufferedWriter(FWriter);
                    String UrlData = null;
                    while ((UrlData = BufData.readLine()) != null)
                    {
                          BWriter.write(UrlData);
                          BWriter.newLine();
                    }
                    BWriter.close();
              }
              catch(IOException io)
              {
                   System.out.println(io);
              }
      }
} 

But I need the file have the same name as the page of the website, for example, it has to somehow get the name of the web page and assign it as the file’s name.

  • 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-14T15:41:55+00:00Added an answer on June 14, 2026 at 3:41 pm

    You can use URL.getFile() to get the filename. I.e

    ...
    String htmlFileName = PageURL.getFile();
    ...
    

    Note that different URLs may refer to the same file: http://example.com/test.html#anch1, http://example.com/test.html, http://example.com/test.html?a=b – all three refer to the same test.html file here. In this case you might want to combine getFile(), getRef() and getQuery() somehow.

    It is worth to mention some issues in your code:

    1. start variable names with lowerCase instead of UpperCase;
    2. Close resources in finally blocks. Better, if you use Java 7, use try-with-resources.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that saves an object to a file... -(int) saveObject:
I was trying out the following code which actually saves the pdf file to
I'm using the following code in an ASP.NET page to create a record, then
I've written following code to get the content from a web page and save
I built the following code to output XML: public static XDocument Serialize<T>(this T source)
All, I'm using the following code: <head> <title>Wedding Page</title> <meta http-equiv=Content-Type content=text/html; charset=utf-8 />
I have saved an html page using the following line of code : NSURL
I've seen the following code that takes a screenshot and saves it as jpg,
The Current Setup: So, I can use the following code to save a file
I have the following html code: <div class= modal id=indexModal> <span class=thing></span> <a href=#

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.