How do I read / download webpage content?
Category: java.net, viewed: 1062 time(s).
You want to create a program that reada a webpage content from the internet. The example below using the URL class to create a connection to the website. When you get the connection to a website you can read the stream and write the data to a file.
package org.kodejava.example.net; import java.io.*; import java.net.MalformedURLException; import java.net.URL; public class UrlReadPageDemo { public static void main(String[] args) { try { URL url = new URL("http://www.kodejava.org"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); BufferedWriter writer = new BufferedWriter(new FileWriter("data.html")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); writer.write(line); writer.newLine(); } reader.close(); writer.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
Can't find what you are looking for? Join our FORUMS and ask some questions!