View Single Post
  #5 (permalink)  
Old 10-21-2007, 11:51 PM
amansundar amansundar is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 325
amansundar is on a distinguished road
Exclamation Re: get a URL from cmd line input.

sure...

Code:
import java.net.HttpURLConnection;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;  // needed for BufferedReader, InputStreamReader, etc.
import java.net.*;
 
 
public class URL {
	 
 
	// Create a single shared BufferedReader for keyboard input
	
	private static BufferedReader stdin = 
        new BufferedReader( new InputStreamReader( System.in ) );
 
	public static void main(String[] args)throws IOException {
	
	// Prompt the user
    System.out.print( "Please type a URL with Proxy (i.e. http://www.sitename.com " );
 
    // Read a line of text from the user.
    String input = stdin.readLine();
    
    //convert String to URL
    
    URL myURL=new URL(input);
    
    //connect to URL
 
    	URL url;
        StringBuilder html = new StringBuilder();
    	HttpURLConnection c = (HttpURLConnection)url.openConnection();
	    BufferedInputStream in = new BufferedInputStream(c.getInputStream());
	    Reader r = new InputStreamReader(in);	
 
	    int i;
	    while ((i = r.read()) != -1) {
	    	html.append((char) i);
	    }
	    html.trimToSize();
	   
	    try{
	        // Create file
	        FileWriter fstream = new FileWriter("out.txt");
	            BufferedWriter out = new BufferedWriter(fstream);
	        out.write(html);//write string html to file
	        //Close the output stream
	        out.close();
	        }catch (Exception e){//Catch exception if any
	          System.err.println("Error: " + e.getMessage());
	        } 
 
	}// end main method
}//end class
__________________
cheers
Aman
Reply With Quote