Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2006, 05:36 PM   PM User | #1
YodaCows
New to the CF scene

 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
YodaCows is an unknown quantity at this point
Exclamation Java - Output to 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:

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

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

~YC
YodaCows is offline   Reply With Quote
Old 02-18-2006, 06:19 PM   PM User | #2
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
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?
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 02-21-2006, 10:35 PM   PM User | #3
YodaCows
New to the CF scene

 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
YodaCows is an unknown quantity at this point
Quote:
Originally Posted by Spookster
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?
I see what you mean....each time its just going to print the current line, instead of appending it to the end.

I've tried to move the file/Output stuff outside the while but that doesn't work.

I tried this
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 )
         {
            Output = new FileOutputStream("myfile.txt");
         }
      }
      catch( IOException e )  // catch any errors
      {
         System.out.println( "There was an IOException error!" );
      }
      file = new PrintStream(Output);
      file.println(line);
   }
}
but that doesn't work either. It runs but give NullPointerExceptions and it prints null in myfile.txt

I don't know how to break out of the loop, or move the code out of the loop
I'm looking at these sites (1,2) but I cant get this thing to run.
Any more hints?

~YC
YodaCows is offline   Reply With Quote
Old 02-21-2006, 11:04 PM   PM User | #4
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Ok I guess my hint wasn't good enough.

Ok here:

Output = new FileOutputStream("myfile.txt");

You have this inside the while loop. With each iteration of the loop you were creating a new instance of that file and essentially overwriting what was written to it the previous iteration. That is why your original code was only writing the last line to the text file.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 02-21-2006, 11:49 PM   PM User | #5
YodaCows
New to the CF scene

 
Join Date: Jan 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
YodaCows is an unknown quantity at this point
I tried removing the while loop, but now it doesn't print anything to the file
Code:
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;
         Socket s = new Socket( "www.yahoo.com", 80 );
         br = new BufferedReader( new InputStreamReader( s.getInputStream() ));
         bw = new BufferedWriter( new OutputStreamWriter( s.getOutputStream() ));

         bw.write( "GET / HTTP/1.0\n\n" );
         bw.flush();

         Output = new FileOutputStream("myfile.txt");
         file = new PrintStream(Output);
         file.println (line);
        }
      catch( IOException e )  // catch any errors
      {
         System.out.println( "There was an IOException error!" );
      }
   }
}
I've tried every combination...moving the file/Output lines all over.
What am I not doing right (besides everything)?

~YC
YodaCows is offline   Reply With Quote
Old 02-22-2006, 01:50 AM   PM User | #6
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Your heading in the wrong direction now. The original code was fine for the most part. You need to understand what is going on with each line of code if you want to do this correctly. Just moving lines of code around in hopes that things will start magically working is not the way to learn to program. Been there done that, it doesn't work.

You should go through the original code and learn what each method is doing and what is happening when you create new objects or why you need to create the objects. Learn what each line of code means and what it does and why we need to use it.

Here is the original code that you posted with comments to describe what is happening:

PHP Code:

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

public class 
scrapeSite
{
   public static 
void mainString[] args )
   {

     
// you need enclose certain types of code in try catch blocks
    //  that might generate exceptions.  Literally Try to do this but if it
    // something goes wrong Catch the exception and do something else instead
      
try
      {
         
// data member declarations
         
String line// a string variable
         
BufferedReader br// buffered reader object
         
BufferedWriter bw// a file output stream object  

        // data member declaration and object instantiation in one line
         // create a connection to 'www.yahoo.com' on port 80
         
Socket s = new Socket"www.yahoo.com"80 ); // Socket object is declared and instantiated (created)

         // create the reader and writer objects
         
br = new BufferedReader( new InputStreamReaders.getInputStream() )); // Buffered reader object is created and passed a parameter of a new InputStreamReader object which is passed a parameter of the socket object we created earlier

         
bw = new BufferedWriter( new OutputStreamWriters.getOutputStream() )); // Buffered writer object is created and passed a parameter of a new OutputStreamWriter object which is passed a parameter of the socket object we created earlier

         // request the 'root' page
         
bw.write"GET / HTTP/1.0\n\n" ); // we write this string to output stream
         
bw.flush(); // we now flush what was in the output stream

         // while more lines, output to the standard output stream - Duh
         // so keep looping and executing what is inside this loop until you reach the last line of text that gets read in
         
while( (line br.readLine()) != null )
         {
            
System.out.printlnline ); // self explanatory
         
}
      }
      catch( 
IOException e )  // catch any errors - Duh
      
{
         
System.out.println"There was an IOException error!" );
      }
   }

Now in your modified code you added in objects to output the text that is being read in to write it to a file instead of printing it to the screen. The problem you ran into though is that your output file only contained the last line of text that was read in. Why is that? Your code was actually reading in every line of text from that website and it was even writing every line to that output file. The problem is you were overwriting the file each time the while loop ran. You were creating new instances of the FileOutputStream and PrintStream every time the loop executed. Do you need to create a new instance of these object with every iteration of the loop? No. You only need to create one instance. Once those objects are created you can use the methods of those objects to process the incoming lines and write them to the output file. Just follow your original example.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 02-22-2006, 01:53 AM   PM User | #7
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,222
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
You should also learn to understand the scope of data members. Do you really need to make all your data members public and static? Not necessarily. Learn what public means and when to use it and what static means and when to use it. Learn where and when you should declare and instantiate objects. Leanr what the methods of these object are for and how they work. You have to understand the code if you want to learn to program.
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:14 AM.


Advertisement
Log in to turn off these ads.