Results 1 to 2 of 2
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Simply output the result to a text file.

    This is an assignment, so I'm just looking for a hint.
    Hopefully someone can help, it seems like it should be easy.

    This is the original code:

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class scrapeSite
    {
       public static void main( String[] args )
       {
          try
          {
             String line;
             BufferedReader br;
             BufferedWriter bw;
    
             // create a connection to 'www.yahoo.com' on port 80
             Socket s = new Socket( "www.yahoo.com", 80 );
    
             // create the reader and writer objects
             br = new BufferedReader( new InputStreamReader( s.getInputStream() ));
             bw = new BufferedWriter( new OutputStreamWriter( s.getOutputStream() ));
    
             // request the 'root' page
             bw.write( "GET / HTTP/1.0\n\n" );
             bw.flush();
    
             // while more lines, output to the standard output stream
             while( (line = br.readLine()) != null )
             {
                System.out.println( line );
             }
          }
          catch( IOException e )  // catch any errors
          {
             System.out.println( "There was an IOException error!" );
          }
       }
    }
    My assignment is to simply output the result to a text file. After looking through some books and other tutorials, I've come up with this

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class scrapeSite
    {
        public static FileOutputStream Output;
        public static PrintStream file;
        public static String line;
    
       public static void main( String[] args )
       {
          try
          {
             BufferedReader br;
             BufferedWriter bw;
    
             // create a connection to 'www.yahoo.com' on port 80
             Socket s = new Socket( "www.yahoo.com", 80 );
    
             // create the reader and writer objects
             br = new BufferedReader( new InputStreamReader( s.getInputStream() ));
             bw = new BufferedWriter( new OutputStreamWriter( s.getOutputStream() ));
    
             // request the 'root' page
             bw.write( "GET / HTTP/1.0\n\n" );
             bw.flush();
    
             // while more lines, output to the standard output stream
             while( (line = br.readLine()) != null )
             {
                //System.out.println( line );            
                Output = new FileOutputStream("myfile.txt");
                // Connect print stream to the output stream
                file = new PrintStream(Output);
                file.println (line);
             }
          }
          catch( IOException e )  // catch any errors
          {
             System.out.println( "There was an IOException error!" );
          }
       }
    }
    It almost works, except only the last line gets writtent to the txt file.
    Am I on the right track?

    Thanks

  2. #2
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Take a look at your while loop and what is going to happen each time that loop is executed. You are going to create a new file output and print stream object with each iteration of the loop. Is that enough of a hint?
    Greetings.

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. Replies: 2
    Last Post: 04-07-2009, 07:25 AM
  3. Text Output for a library
    By dream_noir in forum Advanced Java
    Replies: 0
    Last Post: 04-14-2008, 06:31 AM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  5. Replies: 1
    Last Post: 07-24-2007, 03:02 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
  •