Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0

    Webpage saver ...


    Hey im kind new in this , but I have coded this but It doesn't work...

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;

    public class webpagesaver{
    public static void main(String[] args) throws IOException{
    URLConnection conn = url.openConnection();
    conn.connect();
    InputStream is = conn.getInputStream();
    String saveFile = "PaginaCopiada.html"; //name of SaveFile
    String location = "ht#t#p://ww#w.#sa#p#o.com"; // website url withou #
    URL url = new URL(location);
    BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    String l = out.readLine();
    while(l!=null){
    out.write(l);
    out.newLine();
    }
    in.close();
    out.close();

    }
    }
    The objective is : Put website link and Save all the page..

    May you help me ?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    It doesn't work...
    Can you explain what "doesn't work" means?
    If you get error messages, please copy the full text and paste it here.
    If the program executes but the results are unexpected, please show what it does and explain.

    Please edit your post and wrap the code in code tags to preserve formatting.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Code:
     
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class webpagesaver{
    public static void main(String[] args) throws IOException{
    
    
    String saveFile = "PaginaCopiada.html"; //name of SaveFile
    String location = "http://www.sapo.pt"; // website url withou #
    URL url = new URL(location);
    URLConnection conn = url.openConnection();
    InputStream is = conn.getInputStream();
    conn.connect();
    BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    String l = out.readLine();
    while(l!=null){
    out.write(l);
    out.newLine();
    }
    in.close();
    out.close();
    
    }
    }
    Compiling messages

    Code:
    URLConnection conn = url.openConnection();
    ^
      symbol:   class URLConnection
      location: class webpagesaver
    webpagesaver.java:19: error: cannot find symbol
    InputStream is = conn.getInputStream();
    ^
      symbol:   class InputStream
      location: class webpagesaver
    webpagesaver.java:23: error: cannot find symbol
    String l = out.readLine();
                  ^
      symbol:   method readLine()
      location: variable out of type BufferedWriter
    3 errors
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Check that there are import statements for the classes referenced in the error messages.

    The code needs formatting. Logically nested statements should be indented 3-4 spaces and not start in the first column.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    But the idea is that ? Im on the right way ?
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Does the code compile and execute now? It needs to have a clean compile before it can be executed and tested.

    It is hard to read and understand unformatted code.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Ty, I checked and corrected

    Code:
    import java.io.*;
    import java.net.*;
     
    public class webpagesaver{
        public static void main(String[] args) throws IOException{
        
            String saveFile = "PaginaCopiada.html"; //name of SaveFile
            String location = "http://www.iol.pt"; // website url withou #
            URL url = new URL(location);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            conn.connect();
            BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String l = in.readLine();
            while(l!=null){
                out.write(l);
                out.newLine();
            }
            in.close();
            out.close();
        }    
    }
    But with that page it works well but for example with facebook it save a blank page, and another example with www.iol.pt its doesn't stop running and I stopped and checked that it created a 1,5Gb its normal ?
  14. #8
  15. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    How do you read the line from the server? Now how do you read the next line? Hint - debugging 101 might have been to look at the output and realize that you're outputting the same line over and over.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    I dont know , I never work with URL api
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    I never work with URL api
    The problem has nothing to do with the source of the input. The code would have the same problem if it was reading from a disk file.

    Change this line:
    Code:
     InputStream is = conn.getInputStream();
    
    to this:
    
     InputStream is = new FileInputStream("<PUT FILENAME HERE>");
    compile and execute the code to see what happens.
    Replace <PUT FILENAME HERE> with the name of a file on your PC
    Last edited by NormR; January 14th, 2013 at 01:28 PM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    The file with the link website ?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    The test I suggested has nothing to do with the web or any website.
    It was to show you that the problem with the code has nothing to do with the internet.
    The problem will be the same if you try to read a file from your PC.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Ty for the answer I did this

    Code:
    import java.io.*;
    import java.net.*;
     
    public class webpagesaver{
        public static void main(String[] args) throws IOException{
        
            String saveFile = "PaginaCopiada.html"; //name of SaveFile
            String location = "http://www.iol.pt"; // website url withou #
            URL url = new URL(location);
            URLConnection conn = url.openConnection();
            InputStream is = new FileInputStream("um.txt"); //conn.getInputStream();
            conn.connect();
            BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String l = in.readLine();
            while(l!=null){
                out.write(l);
                out.newLine();
            }
            in.close();
            out.close();
        }    
    }
    But happens the same
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Yes that is what was supposed to happen. That shows you that the problem is in your program and has nothing to do with the internet.

    Did you look in the file that was written?
    The problem was explained by stdunbar in post#8.

    Where does the code read the next record from the file? It needs to do that inside the loop.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    14
    Rep Power
    0
    Code:
    import java.io.*;
    import java.net.*;
     
    public class webpagesaver{
        public static void main(String[] args) throws IOException{
        
            String saveFile = "PaginaCopiada.html"; //name of SaveFile
            String location = "http://www.iol.pt"; // website url withou #
            URL url = new URL(location);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            conn.connect();
            BufferedWriter out = new BufferedWriter(new FileWriter(saveFile));
            BufferedReader in = new BufferedReader(new InputStreamReader(is));
            String l = in.readLine();
            while(l!=null){
                out.write(l);
                out.newLine();
                in.next(); //<-- this ?
            }
            in.close();
            out.close();
        }    
    }
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo