saving html file to hard drive

D

Drew

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
 
T

Thomas Weidenfeller

Drew said:
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
 
K

klynn47

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.
 
J

John C. Bollinger

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.
 
D

Drew

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

Drew
 
D

Drew

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

Thanks
Drew
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,167
Latest member
SusanaSwan
Top