Results 1 to 6 of 6

Thread: Download and save a web page

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Download and save a web page

    Hi,
    I am trying the java input and the output programs for my practice, I have done handling with the files, now I want to know how can I download and save the web page on my local drive. I am not sure that my code works properly for saving the file. I am trying it, but if you have any code or part of a code which can give me any idea about how to do it, then it would be great for me.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Download and save a web page

    Hello,
    I had done a program for performing the same task that you are asking for, just have a look at the code below.
    Code:
    try{
      BufferedReader buf = new BufferedReader(new FileReader(new    Url('url "));
      Localfile FileWriter = new FileWriter (new File ( "local"));
    
      String s;
    
      while ((s = buf.readLine ())! = null) (
         localFile.write (s);
      )
      buf.close ();
      localFile.close ();
    )
    catch (Exception e) (
      System.out.println ( "Error:" + e);
    )

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Download and save a web page

    Hello,
    The above is correct, this is one more way of doing the same.
    Code:
    Public class Test
    {
       Public static void hand(String args[])
       {
         
    		try{
    		  BufferedReader buf = new BufferedReader(new InputStreamReader(new URL("url").openStream()));
    FileWriter fw = new FileWriter(new File("file"));
    
    String s;
    		
    		  while((s = buf.readLine()) != null){
    		     fw.write(s);
    		  }
    		  buf.close();
    fw.close();
    		}
    		catch(Exception e){
    		  System.out.System.out.println("Error:" + E);
    		}
       }
      
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Download and save a web page

    Hello,
    Just an observation but it is important, always do the close () in a block finally. To properly close the streams even in cases of emergency. Otherwise we can find to keep files open when we have no more object references. For an application server or intended to operate for a relatively long time it can cause problems. Have a look below.
    Code:
    Public class Test
    {
       Public static void hand(String args[])
       {
          BufferedReader buff = null;
          FileWriter fw = null;
         
          try{
            buff = new BufferedReader(new InputStreamReader(new URL("url").openStream()));
            fw = new FileWriter(new File("file"));
          
            String s;
          
            while((s = buff.readLine()) != null){
               fw.write(s);
            }
          }
          catch(Exception e){
            System.out.System.out.println("Error:" + E);
          } finally {
     
            if (buff! =null) {
                try {
                    buff.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
     
            if (fw! =null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
     
          }
       }
     
    }

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Download and save a web page

    Hello,
    Yes I completely agree with the above, it is always a good practice to use the try and the catch block in your code, for safety reasons.
    Code:
    Public static void trycl (Closeable closeable) {
    	if (closeable! =null) {
    		try {
    			closeable.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    This then gives:
    Code:
    finally {
    	Test.trycl(urlReader);
    Test.trycl(localfile);
    }
    Last edited by kelfro; 23-01-2010 at 01:14 PM.

  6. #6
    Join Date
    Dec 2009
    Posts
    192

    Re: Download and save a web page

    Hello,
    Thank you for your reply, I tried with an ip address, but it giving I me an error message
    "Error: java.net.NoRouteToHostException: Operation timed out:
    I think it may be because I go through a proxy server, knowing that I am in a network local. Do you guys have any idea about the following error, then please reply for the same or suggest me what methods can I use.

Similar Threads

  1. Just Cause 2 save game download pc
    By ricardoramey in forum Video Games
    Replies: 13
    Last Post: 26-04-2011, 04:09 PM
  2. Kindle 2 doesn't save last page read
    By Gadin in forum Technology & Internet
    Replies: 4
    Last Post: 19-03-2010, 06:09 PM
  3. how to save a web page as a PDF file?
    By Renfred in forum Windows Software
    Replies: 3
    Last Post: 10-01-2009, 09:20 PM
  4. Save the Web page images in a single file
    By urgaffel in forum Technology & Internet
    Replies: 2
    Last Post: 26-11-2008, 05:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,645,626.78111 seconds with 17 queries