Java Reference
In-Depth Information
import java.io.*;
import java.net.*;
public class DownloadBinary
{
// the size of a buffer
public static int BUFFER_SIZE = 8192;
/**
* This method downloads the specified URL into a Java
* String. This is a very simple method, that you can
* reused anytime you need to quickly grab all data from
* a specific URL.
*
* @param url The URL to download.
* @return The contents of the URL that was downloaded.
* @throws IOException Thrown if any sort of error occurs.
*/
public String downloadPage(URL url) throws IOException
{
StringBuilder result = new StringBuilder();
byte buffer[] = new byte[BUFFER_SIZE];
InputStream s = url.openStream();
int size = 0;
do
{
size = s.read(buffer);
if (size != -1)
result.append(new String(buffer, 0, size));
} while (size != -1);
return result.toString();
}
public void saveBinaryPage(String filename, String page)
throws IOException
{
OutputStream os = new FileOutputStream(filename);
os.write(page.getBytes());
os.close();
}
Search WWH ::




Custom Search