Results 1 to 3 of 3
  1. #1
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default download files from internet

    im trying to write a program to download files from the internet but its saying that the constructor needs a class after public. here is the source code and error report.
    Java Code:
    import java.io.*;
    import java.net.*;
    public class Downloader {
    	public Downloader{
    					try{
    						InetAddress proxyAddress = InetAddress.getByName("");
    						InetSocketAddress inetSocketAddress = new InetSocketAddress(proxyAddress, 80);
    						Proxy proxy = new Proxy(Proxy.Type.HTTP, inetSocketAddress);	        
    						URL url = new URL("");
    						URLConnection urlConnection = url.openConnection();
    						urlConnection.connect();
    						InputStream input = url.openStream();
    						FileWriter fw = new FileWriter("DownloadFile31MAY2005.txt");
    						Reader reader = new InputStreamReader(input);
    						BufferedReader bufferedReader = new BufferedReader(reader);
    						String strLine = "";
    	                     int count = 0;
    	                     while(count < 10000){
    	                           strLine = bufferedReader.readLine();
    	                           if(strLine != null && strLine != ""){                
    	                        	   fw.write(strLine);
    	                           }
    	                           count++;
    	                     }
    					} catch ( Exception e ){
    						e.printStackTrace();
    					}
    	}
    	}
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Syntax error on token "public", class expected after this token
    
    	at Downloader.<init>(Downloader.java:4)
    	at MainC.main(mainC.java:8)

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default

    You need parentheses after your constructor name. It's a method.

    Another problem I see: you catch exceptions thrown by your constructor. You shouldn't do that. (Or at least, re-throw them after you catch them if they're not recoverable.) An exception propagating out of a constructor means the caller never gets a reference to the incomplete/invalid object that was being constructed when the exception was generated. Constructors should always leave the object in a complete and valid state, or no state at all.
    Last edited by kjkrum; 07-30-2011 at 10:53 PM.
    Get in the habit of using standard Java naming conventions!

  3. #3
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default

    wow how did i miss that i looked over the code for 30 min. and really i didnt know that thanks for the advice.

Similar Threads

  1. Get Internet Adresses
    By argus in forum New To Java
    Replies: 1
    Last Post: 10-28-2010, 09:32 PM
  2. RMI through Internet.???
    By bach_meoden in forum Networking
    Replies: 8
    Last Post: 05-19-2010, 02:48 PM
  3. My Web App on Internet
    By orion_mcl in forum Advanced Java
    Replies: 1
    Last Post: 03-14-2010, 09:13 PM
  4. Replies: 2
    Last Post: 03-28-2009, 06:50 AM
  5. Replies: 1
    Last Post: 11-06-2008, 04:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •