Voqus Posted May 22, 2012 Posted May 22, 2012 Hello once again, im sure you know some programms out there that actually leech site's html codes. So that's what ive made, but it only takes the html code from the url u have entered and saves it to the file requested not the images. package com.test; import java.io.*; import java.net.URL; /** * @author Voqus */ public class HTMLeech { public static void main(String args[]) { try { URL url = new URL("http://www.google.gr"); // Read the text returned from the site BufferedReader br = new BufferedReader(new InputStreamReader( url.openStream())); String htmlcode; while ((htmlcode = br.readLine()) != null) { try { FileWriter fw = new FileWriter("HTMCode.txt"); BufferedWriter bw = new BufferedWriter(fw); while ((htmlcode = br.readLine()) != null) { bw.write(htmlcode + "\n"); } bw.close(); } catch (IOException e) { e.printStackTrace(); } } br.close(); } catch (IOException e) { e.printStackTrace(); } } } Screen: Tadaaaa! Credits: Me Quote
Setekh Posted May 23, 2012 Posted May 23, 2012 You could of just done that. Every byte in that input stream is a char, your case is the worst case to use a buffer reader. *Sigh*, you only needed 1 output stream writer not two >.> URL url = new URL("http://www.google.gr"); InputStream io = url.openStream(); byte[] data = new byte[io.available()]; io.read(data); String result = new String(data); Quote
Voqus Posted May 23, 2012 Author Posted May 23, 2012 URL url = new URL("http://www.google.gr"); InputStream io = url.openStream(); byte[] data = new byte[io.available()]; io.read(data); String result = new String(data); System.out.println(result); Well yea that's true but ur code just prints out on the console not creating a file and write it. Anyway thx. Quote
Setekh Posted May 24, 2012 Posted May 24, 2012 URL url = new URL("http://www.google.gr"); InputStream io = url.openStream(); byte[] data = new byte[io.available()]; io.read(data); String result = new String(data); System.out.println(result); Well yea that's true but ur code just prints out on the console not creating a file and write it. Anyway thx. I gave you the string, then you can write it. Take a look: URL url = new URL("http://www.google.gr"); InputStream io = url.openStream(); byte[] data = new byte[io.available()]; io.read(data); FileOutputStream fs = new FileOutputStream(new File("index.html")); fs.write(data); fs.flush(); Later edit: Btw, i didn't see you use flush in your out stream writers, you always have to flush the data out after writing something in the stream, that's what saves it on your disk :) Also buffer reader is used only when you wanna get a collection of strings, or parse an csv file, and so forth. And in a text file every byte in there is a char. And finally the way a buffer reader is needed to eb set up is like this: BufferedReader br = new BufferedReader(new InputStreamReader(io)); while(br.ready()) { String line = br.readLine(); } It's exactly like in l2js db connections, "while(rs.next);"; Hope it helps :) Quote
Voqus Posted May 24, 2012 Author Posted May 24, 2012 I gave you the string, then you can write it. Take a look: URL url = new URL("http://www.google.gr"); InputStream io = url.openStream(); byte[] data = new byte[io.available()]; io.read(data); FileOutputStream fs = new FileOutputStream(new File("index.html")); fs.write(data); fs.flush(); Later edit: Btw, i didn't see you use flush in your out stream writers, you always have to flush the data out after writing something in the stream, that's what saves it on your disk :) Also buffer reader is used only when you wanna get a collection of strings, or parse an csv file, and so forth. And in a text file every byte in there is a char. And finally the way a buffer reader is needed to eb set up is like this: BufferedReader br = new BufferedReader(new InputStreamReader(io)); while(br.ready()) { String line = br.readLine(); } It's exactly like in l2js db connections, "while(rs.next);"; Hope it helps :) aham yep, i can see it now thanks ;D. Btw i'm not really into l2j crap so dont tell about how they use trove and stuff ::) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.