If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

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?

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.

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.

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:

// 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 InputStreamReader( s.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 OutputStreamWriter( s.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

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.

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.