Results 1 to 4 of 4
  1. #1
    trkece is offline Member
    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    Default how to write URL content to a file

    import java.net.*;
    import java.io.*;

    public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
    URL yahoo = new URL("http://www.yahoo.com/");
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);
    in.close();
    }
    }


    It's printing me as a output at the console and i need the same output in a text file.........

    pls where do i need to modify

    pls help me out .......

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Use a bufferedwriter and it's write method (and its newLine method) rather than printing to system.out, of course.

    Edit: P.S. What the <bleep/> does this have to do with Databases/JDBC?

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    You will find a lot of info on what you need to write to a file in the following tutorial: Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
    Google is also a powerful tool, as there are countless tutorials and examples on using a BufferedWriter

  4. #4
    gcurrier is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Write stream to a file

    This works. I can't attest to it being the best way, but it works. And this post really does belong in something besides "Databases". However, if you are doing this to find a way to stick the saved page into a database...

    Java Code:
    package ProjectTesting;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class URLConnectionReader {
    
        public static String outFile = "webpage.html";
    
        public static void main(String[] args) throws Exception {
            URL yahoo = new URL("http://www.yahoo.com/");
            URLConnection yc = yahoo.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
            String inputLine;
            BufferedWriter psout = new BufferedWriter(new FileWriter(outFile));
            
            while ((inputLine = in.readLine()) != null) {
                psout.write(inputLine);
                System.out.println(inputLine);
            }
            in.close();
            psout.close();
        }
    }

Similar Threads

  1. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  2. Replies: 10
    Last Post: 05-24-2010, 08:25 PM
  3. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  4. How to read content from a file
    By eden83 in forum New To Java
    Replies: 2
    Last Post: 01-12-2010, 10:25 PM
  5. How to Encrypt content of txt file?
    By aRTx in forum New To Java
    Replies: 0
    Last Post: 03-25-2009, 10:26 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
  •