saving html file to hard drive

Discussion in 'Java' started by Drew, Feb 23, 2005.

  1. Drew

    Drew Guest

    Hi All:

    I need to be able to prompt a user for a URL in a Java program and
    then have that web page saved to a HTML file on the local hard disk.
    I've done this in VB before but I'm required to use Java for this
    project. Is this possible to do. Note that I do not need to save the
    pictures or other files that the URL refers to. Just the simple HTML
    file is all I need to save to hard disk.

    Is this possible?

    Drew
    Drew, Feb 23, 2005
    #1
    1. Advertising

  2. Drew wrote:
    > I need to be able to prompt a user for a URL in a Java program and
    > then have that web page saved to a HTML file on the local hard disk.
    > I've done this in VB before but I'm required to use Java for this
    > project. Is this possible to do. Note that I do not need to save the
    > pictures or other files that the URL refers to. Just the simple HTML
    > file is all I need to save to hard disk.


    Sounds like you want the URLConnection class.

    /Thomas

    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    Thomas Weidenfeller, Feb 23, 2005
    #2
    1. Advertising

  3. Drew

    Guest

    Use java.net.URL. There is a method called openStream which returns a
    reference to the InputStream of the URL. You can then wrap an
    InputStreamReader around it, and then wrap a BufferedReader around the
    InputStreamReader and read like you would a file.
    , Feb 23, 2005
    #3
  4. wrote:

    > Use java.net.URL. There is a method called openStream which returns a
    > reference to the InputStream of the URL. You can then wrap an
    > InputStreamReader around it, and then wrap a BufferedReader around the
    > InputStreamReader and read like you would a file.


    Or, if you want to do it correctly, either
    (1) forget about the Reader and just copy the bytes directly from the
    input stream to a suitable output stream, preferably via a buffer for
    performance, or
    (2) use URL.openConnection() to obtain a URLConnection, then the return
    value of URLConnection.getContentEncoding() to construct an
    InputStreamReader that will correctly decode the content bytestream into
    characters; in this case you should also make sure to specify the
    charset to use when writing the content back out to a file; you must
    also watch out for unspecified encoding (in which case you should assume
    ISO-8859-1). Even this much assumes that the entity referred to by the
    URL is of some subtype of text.

    Option (1) is much easier to do right than option (2), but it loses all
    external information about the charset with which the entity is encoded.

    --
    John Bollinger
    John C. Bollinger, Feb 23, 2005
    #4
  5. Drew

    Drew Guest

    Thanks to the both of you, Klynn74 and Thomas. Your advice was dead
    on and was exactly what I needed. Much appreciated!

    Drew



    On 23 Feb 2005 06:47:24 -0800, wrote:

    >Use java.net.URL. There is a method called openStream which returns a
    >reference to the InputStream of the URL. You can then wrap an
    >InputStreamReader around it, and then wrap a BufferedReader around the
    >InputStreamReader and read like you would a file.
    Drew, Feb 23, 2005
    #5
  6. Drew

    Drew Guest

    For my purposes, the assumption that the URL is text is fine. I am
    using .openConnection and InputStreamReader. It works fine.

    Thanks
    Drew


    On Wed, 23 Feb 2005 12:07:28 -0500, "John C. Bollinger"
    <> wrote:

    > wrote:
    >
    >> Use java.net.URL. There is a method called openStream which returns a
    >> reference to the InputStream of the URL. You can then wrap an
    >> InputStreamReader around it, and then wrap a BufferedReader around the
    >> InputStreamReader and read like you would a file.

    >
    >Or, if you want to do it correctly, either
    >(1) forget about the Reader and just copy the bytes directly from the
    >input stream to a suitable output stream, preferably via a buffer for
    >performance, or
    >(2) use URL.openConnection() to obtain a URLConnection, then the return
    >value of URLConnection.getContentEncoding() to construct an
    >InputStreamReader that will correctly decode the content bytestream into
    >characters; in this case you should also make sure to specify the
    >charset to use when writing the content back out to a file; you must
    >also watch out for unspecified encoding (in which case you should assume
    >ISO-8859-1). Even this much assumes that the entity referred to by the
    >URL is of some subtype of text.
    >
    >Option (1) is much easier to do right than option (2), but it loses all
    >external information about the charset with which the entity is encoded.
    Drew, Feb 25, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mh
    Replies:
    4
    Views:
    700
    Wolfgang Strobl
    May 31, 2005
  2. shailesh
    Replies:
    1
    Views:
    780
    Tim Golden
    Mar 28, 2007
  3. Peter
    Replies:
    8
    Views:
    159
    Evertjan.
    Oct 15, 2007
  4. michael
    Replies:
    1
    Views:
    90
    Bret Pettichord
    Oct 15, 2006
  5. Alex
    Replies:
    2
    Views:
    226
    Thomas 'PointedEars' Lahn
    Mar 1, 2006
Loading...

Share This Page